JavaScript implementation Inheritance

Source: Internet
Author: User

In Java and C #, you can simply understand that class is a mold, and the object is a batch of mooncakes that are pressed out of the mold. What kind of pressure, you have to be a kind of, can not move, move on the bad.
And in JavaScript, without a mold, the moon cake is replaced with dough, and you can squeeze it into the way you want it. object Properties, methods not appropriate, can be modified. There are no methods that can be created on their own and play a great degree of freedom.

There are no classes in JavaScript, but you can use functions as classes so that functions that are used as class constructors are called Constructors .
Constructor, the first letter of the function name is capitalized, and this is used internally. using the new operator on the constructor will generate the instance, and this will point to the instance object.

Many object-oriented languages support two ways of inheriting: interface inheritance and implementation inheritance .
Interface Inheritance: Only the method signature is inherited;
Implement inheritance: Inherit the actual method.
JavaScript itself is not an object-oriented language, but an object-based language. In ECMAScript, it is not possible to implement interface inheritance, only support implementation inheritance , and implementation of inheritance is mainly dependent on the prototype chain . Each function has a call,apply method, which has properties such as Length,arguments,caller. Why does each function have? This must be "inherited". Functions are created by function functions and therefore inherit methods from Function.prototype.
ES5 inheritance is to create the instance object of the subclass this first, and then add the parent class to the This object method;
ES6 inheritance is to first create an instance object of the parent class this, and then modify this with the constructor of the subclass.

ECMAScript inheritance is divided into two major categories: Reference object inheritance , instance Object inheritance.
One, Reference object inheritance
The child reference type inherits the parent reference type and then the instance object that is generated by the child reference type, with attributes of the parent reference type.

JavaScript implementation Inheritance

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.