Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nested ES6 classes?

It seems possible to nest a class in a constructor which can then be instantiated from anywhere within the class, is this official?

[EDIT] E.g.,

class C {      constructor() {         class D {             constructor() { }         }     }      method() {         var a = new D();  // works fine     }  }  //var a = new D();  // fails in outer scope 

The traceur generated JS https://google.github.io/traceur-compiler/demo/repl.html

$traceurRuntime.ModuleStore.getAnonymousModule(function() {   "use strict";   var C = function C() {     var D = function D() {};     ($traceurRuntime.createClass)(D, {}, {});   };   ($traceurRuntime.createClass)(C, {method: function() {       var a = new D();     }}, {});   return {}; }); //# sourceURL=traceured.js 
like image 723
user5321531 Avatar asked Feb 28 '15 16:02

user5321531


People also ask

Can you have nested classes in JavaScript?

Let us try to understand, what is class. A class in JavaScript is a type of function, which can be initialized through both function keyword as well as through class keyword. In this article, we will cover the inner class in javascript through the use of a function keyword.

What are the four types of nested classes?

As a member of the OuterClass , a nested class can be declared private , public , protected , or package private.

Can you define a class within a class in JavaScript?

We define the class members inside the brackets. The body of the class is executed in strict mode, so everything defined in strict mode applies to the definition of a class, so we can't define variables without keyword before it like var , let or const , and many other rules apply when you define a class.

What are classes in ES6?

There are two types of Class in ES6: parent class/super class: The class extended to create new class are know as a parent class or super class. child/sub classes: The class are newly created are known as child or sub class. Sub class inherit all the properties from parent class except constructor.

Is it possible to have two classes in ES6?

No, there are no nested classes in ES6, and there is no such thing as private members in the class syntax anyway if you mean that. Of course you can put a second class as a static property on another class, like this: class A { … } A.B = class { …

What is the difference between class and constructor in ES5?

On the other hand, expanding the inner __proto__ for the instance created with the constructor function gives the result as: This means the instance created by the ES5 constructor function is inheriting from JavaScript’s object while the instance created with the class syntax is not inheriting any of the properties from JavaScript’s object class.

How to implement simple inheritance in ES6?

With classes, we get no inheritance by default and we get a pure object assigned to the inner __proto__ property of the User object’s prototype. We can also do that by explicitly modifying the inner __proto__ property. Object.create (null) creates a pure object for us. Implementing a simple inheritance in ES6 would look like the example below.

Are classes just syntactical sugar over ES5 code?

See how an instance created for a class is represented internally with prototypes and the object’s key-value pairs. The user instance we’ve just seen is represented by the prototypes and object’s key-value pairs which is the ES5 syntax. So, we can clearly say that classes are just syntactical sugar over ES5 code.


1 Answers

No, there are no nested classes in ES6, and there is no such thing as private members in the class syntax anyway if you mean that.

Of course you can put a second class as a static property on another class, like this:

class A {     … } A.B = class {     … }; 

or you use an extra scope:

var C; {     class D {         constructor() { }     }     C = class C {         constructor() { }         method() {             var a = new D();  // works fine         }     } } 

(There seems to be a bug with traceur as it uses a hoisted var for the class declaration instead of block scope)


With the proposed class field syntax, it will also be possible to write a single expression or declaration:

class A {     …     static B = class {          …     } }; 
like image 146
Bergi Avatar answered Oct 08 '22 05:10

Bergi