Simple inheritance relationship in JavaScript---cocos2d JS

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.