Home >Web Front-end >JS Tutorial >JavaScript prototypal inheritance_js object-oriented

JavaScript prototypal inheritance_js object-oriented

WBOY
WBOYOriginal
2016-05-16 17:57:591100browse

Object.prototype
JavaScript is based on prototype inheritance, and any object has a prototype attribute. Object.prototype is the root of all objects and cannot be changed.

Copy code The code is as follows:

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

Object and Object.prototype
Object inherits from Object.prototype, adding an attribute to Object.prototype, and it will also be reflected on Object. . For example:
Copy code 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
Since Object.prototype is the root of all things, Function.prototype will also inherit all properties of Object.prototype. For example:
Copy code 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 all functions, and functions inherit from Function.prototype, so changing Function.prototype will also affect Object/Function/String/Number/Boolean/Array and Date. For example:
Copy code 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 also be affected by Object.prototype. passed to its next level. For example:
Copy code 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

Copy code The code is as follows:

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

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

Array is a function object, affected by Function.prototype The influence of Array.prototype is not a function object and is not affected by Function.prototype. However, all objects are affected by Object.prototype, so Array.prototype will also be affected by Object.prototype. For example:
Copy code 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
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn