JavaScript prototype inheritance _ js object-oriented-js tutorial

Source: Internet
Author: User
JavaScript prototype inheritance. If you want to learn JavaScript object-oriented code, you can check it out. Object. prototype
JavaScript is inherited based on prototype. Any object has a prototype attribute. Object. prototype is the root of all objects and cannot be changed.

The Code is as follows:


Object. prototype = null;
Alert (Object. prototype); // [object Object]


Object and Object. prototype
The Object inherits from Object. prototype, adds an attribute to Object. prototype, and returns it to the Object. For example:

The Code is as follows:


Object. prototype. nameStr = "Object Prototype ";
Object. prototype. getName = function () {return this. nameStr };
Alert (Object. getName (); // Object Prototype


Function. prototype and Object. prototype
Because Object. prototype is the root of all things, Function. prototype also inherits all attributes of Object. prototype. For example:

The Code is as follows:


Object. prototype. nameStr = "Object Prototype ";
Object. prototype. getName = function () {return this. nameStr };
Alert (Function. prototype. getName (); // Object Prototype



Object/Function/String/Number/Boolean/Array and Date
Object/Function/String/Number/Boolean/Array and Date are both functions, and the functions inherit from the functions. prototype, so change Function. prototype affects Object/Function/String/Number/Boolean/Array and Date. For example:

The Code is as follows:


Function. prototype. initType = 'function type ';
Function. prototype. getType = function () {return this. initType };
// Alert (Object. getType (); // Function Type
// Alert (Date. getType (); // Function Type
// Alert (Number. getType (); // Function Type
// Alert (String. getType (); // Function Type
// Alert (Boolean. getType (); // Function Type
Alert (Array. getType (); // Function Type


Similarly, Function. prototype will pass the impact of Object. prototype to its next level. For example:

The Code is as follows:


Object. prototype. nameStr = "Object Prototype ";
Object. prototype. getName = function () {return this. nameStr };
Alert (Function. prototype. getName (); // Object Prototype
Alert (Array. getName (); // Object Prototype


The Code is as follows:


Alert (Boolean. prototype. getName (); // Object Prototype

Array/Array. prototype and Function. prototype/Object. prototype


Array is a Function object. prototype, while Array. prototype is not a Function object and is not a Function. prototype, but all objects are affected by the Object. the impact of prototype, so Array. prototype is also subject to Object. prototype impact. For example:

The Code is as follows:


Object. prototype. nameStr = "Object Prototype ";
Object. prototype. getName = function () {return this. nameStr };
// Alert (Function. prototype. getName (); // Object Prototype
// Alert (Boolean. prototype. getName (); // Object Prototype
Function. prototype. initFun = function (){
Return 'function. prototype. initFun ';
}
Alert (Array. initFun (); // Function. prototype. initFun
Var arr = ['A', 'B'];
Alert (arr. getName (); // Object Prototype
Alert (arr. initFun (); // Error: arr. initFun is not a function
Alert (arr. initFun); // undefined

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.