ES6 class member to be a class itself [duplicate] - javascript

This question already has answers here:
Nested ES6 classes?
(4 answers)
Closed 6 years ago.
in es6 a class can have methods and static methods as its members. can it also have classes as members? (because Class is actually kind of function itself?)
i would like it to build nested classes.
class foo {
barMethod {
// do something for the instance
}
static bamMethod {
// do something
}
// e.g.
class batClass {
// the constructor and other stuff for batClass
}
}
new foo.batClass();

[No.] (Nested ES6 classes?). Read this post. You can only do as #floribon mentioned in comment.

Related

How to make properties private in classes of Javascript 6? Purpose of setters and getters? [duplicate]

This question already has answers here:
Private properties in JavaScript ES6 classes
(41 answers)
Is the underscore prefix for property and method names merely a convention?
(6 answers)
Closed 5 years ago.
Look at the following code:
class Animal() {
constructor(type, name) {
this._type = type;
this._name = name;
}
get type() {
return this._type;
}
get name() {
return this._name;
}
}
If I were to run:
var Dog = new Animal("Dog", "Rover");
console.log(Dog._name);
The console would return the name of my dog without the need to use the `get name() {} that I wrote above. What is the point of getters and setters in Javascript 6 if I can access my object properties directly? Why is the underscore a convention for private properties when it does nothing to privatize them? How can I make my properties private in classes in Javascript using current ecmascript-6 standards?

How to have a class which is a property of something? [duplicate]

This question already has answers here:
How to namespace es6 classes (for React components)
(3 answers)
Closed 6 years ago.
I have a javascript object menu, and I want to add a property controller which should be a constructor. However, the following gives a syntax error:
class menu.foobar {
// stuff here
}
What is the right way to do this?
Use a class expression:
menu.foobar = class {}

Calling static method from constructor es6 [duplicate]

This question already has answers here:
Call static methods from regular ES6 class methods
(3 answers)
Closed 6 years ago.
When I am trying to call a static method from constructor in javascript it says the method doesn't exist.
class TestClass {
constructor(){
this.staticMethod();
}
static staticMethod() {
}
}
This works fine if i try to call a normal method from constructor. If static methods belongs to class instead of instance why it's not allowing them to call from constructor?
this.constructor.staticMethod()
can be used to avoid referring to the class directly (particularly useful for class inheritance and pasted code).
You have to call it like this:
TestClass.staticMethod()

Creating "static" members in a javascript object [duplicate]

This question already has answers here:
Static variables in JavaScript
(43 answers)
Closed 8 years ago.
Is there a way to create "static" members in a JS Object?
function Person(){
}
Person.prototype.age = null;
Person.prototype.gender = null;
I would like to add personsCount as a static member, is that possible?
Sure, just add Person.personsCount without the prototype
Common practise is to make such "static members" properties of the constructor function itself:
function Person() {
Person.count++;
}
Person.count = 0;

Possible to override a base classes privileged function? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Is there an equivalent of PHP's 'parent' with javascript prototypal inheritance?
Override a base class function
I have a base class/prototype & a child class/prototype in Javascript.
Is it possible to make the child class override a base classes privileged function?
I know I am trying to treat javascript as an OO language which its not but you never know this maybe possible?
If its possible could you give an example how I do this?
function baseClass()
{
this.privFunct() {}
}
function childClass()
{
this.privFunct()
{
var baseFunct = baseClass.prototype.privFunct;
this.baseFunct();
// no do some object specific actions here
}
}

Categories

Resources