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