The JavaScript language itself does not provide a class, there is no other language class inheritance mechanism, its inheritance through the object's prototype implementation, but this does not meet our requirements for the COCOS2D-JS engine, all classes are directly or indirectly inherited implementation.
var person = class.extend ({ //Declaring a person class, inheriting from Class,class.extend () means inheriting from class
Init:function (isdancing) { //define constructor init initialization properties
this.dancing = isdancing;
},
Dance:function () { //define normal function dance (), return property dancing
return this.dancing;
}
});
var Ninja = person.extend ({ //Declaration Ninja class inherits from Person class
Init:function () { ///defines the constructor init, in which the This._super (false) statement is called when the parent class constructor initializes the property in the parent class
This._super (false);
},
Dance:function () { ///Overrides the Dance () function, which overrides the dance () function of the parent class
//call the inherited version of Dance ()
return This._super (); //Call the Dance () function of the parent class
},
Swingsword:function () { //This function is a subclass Ninja newly added function Swingsword ()
return true;
}
});
var p = new Person (true); //Create a P object from the person class, the argument to the constructor is true
Console.log (P.dance ()); //Print P Object Dance Property --True
var n = new Ninja (); //Create n objects through the Ninja class, construct function bits empty, default initialization takes the dance attribute in Fales initialization parent class, prints to False
Console.log (N.dance ()); //false
Console.log (N.swingsword ()); //true
This simple JavaScript inheritance method actually implements the object-oriented concept inheritance and polymorphism mechanism in general sense, which is the core of COCOS2D-JS inheritance mechanism.
Simple inheritance relationship in JavaScript---cocos2d JS