class
// es5 be inherited through the prototype function Pa (X, Y) { // constructor write attribute in this.x = X; this.y from Y =; } // write the public properties or methods prototype Pa.prototype.say function = () { the console.log (in this.x); } // a new new eXAMPLES example methods and constructors will have on the properties of the prototype the let P = Pa new new ( "a", 2); p.say (); // a
// es6 in es5 is different with the same wording // es5 prototype object constructor and write separately, // es6 class is all written in class Pa { // constructor is the constructor es5 in the constructor (x, y) { in this.x = X; this.y from Y =; } // do not need delimiter, direct write prototyping say () { the console.log (in this.x); } } the let P = Pa new new ( "A", 2); p.say (); // A the console.log (== Pa.prototype.constructor Pa); // to true // Pa is that the constructor
Internal methods of the class not enumerable
ES5 // function Pa () {}; Pa.prototype.b = ". 1" the let P = Pa new new (); the console.log (Object.keys (P)); // [] attribute names on the object does not traverse It traverses properties on prototype object console.log (Object.keys (Pa.prototype)); // [ "b"] prototype object is the object listed Key // ES6 class {Ku- C () { Console. log (132); } }; the console.log (Object.keys (Ku.prototype)); // internal [] of all defined class methods are not enumerable
Pa {class } // equivalent to the class {Pa constructor () {} }
class above properties
{Ku-class constructor () { in this.x =. 8; this.y from =. 9; // return the best attributes } A =. 1; // attribute instance directly = @ B: 2 not written c () { the console.log ( "55"); } } the let K = Ku-new new (); the console.log (KA); //. 1 the console.log (KX); //. 8 KC (); // 55 // Object of hasOwnProperty () method returns a Boolean value, determines whether a specific object contains its own (non-inherited) properties. console.log (k.hasOwnProperty ( "x") ); // true own constructor or the console.log (k.hasOwnProperty ( "A")); // to true the console.log (k.hasOwnProperty ( "C ")); // false method on the prototype
Static method
Static method // example which is not inherited, but directly by calling class // used in the former method, plus the static keyword class {Pa static See () { the console.log ( "look"); } say () { the console.log ( "hahaha ~"); } } Pa.see (); // look the let P = Pa new new (); // class needs new new p.say (); // hahaha ~ P. see (); //p.see is not a function instance is not a static method
// static properties // ES6 clear that internal Class only static methods, no static properties. {Pa} class; Pa.a = 1; the let P = Pa new new (); the console.log (Pa.a); // Class 1 directly call the static properties console.log (pa); // undefined example not inherited static Attributes