Object.prototype
JavaScript 是基于原型继承的, 任何对象都有一个 prototype 属性. Object.prototype 是所有对象的根, 并且不可改变.
- Object.prototype=null;
- alert(Object.prototype);//[object Object]
Object 与 Object.prototype
Object 继承于 Object.prototype, 增加一个属性给 Object.prototype 上, 同时也会反应到 Object 上. 如:
- Object.prototype.nameStr="Object Prototype";
- Object.prototype.getName=function(){
- return this.nameStr
- };
- alert(Object.getName());//Object Prototype
Function.prototype 与 Object.prototype
由于 Object.prototype 是万物之根, 所以 Function.prototype 也同时会继承 Object.prototype 的所有属性. 如:
- Object.prototype.nameStr="Object Prototype";
- Object.prototype.getName=function(){
- return this.nameStr
- };
- alert(Function.prototype.getName());//Object Prototype
Object/Function/String/Number/Boolean/Array 与 Date
Object/Function/String/Number/Boolean/Array 与 Date 都是函数, 函数又继承于 Function.prototype, 所以更改 Function.prototype 一样会影响到 Object/Function/String/Number/Boolean/Array 与 Date. 如:
- 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
同样 Function.prototype 也会把所受 Object.prototype 的影响, 传递给它的下一层级. 如:
- Object.prototype.nameStr="Object Prototype";
- Object.prototype.getName=function(){
- return this.nameStr
- };
- alert(Function.prototype.getName());//Object Prototype
- alert(Array.getName());//Object Prototype
- alert(Boolean.prototype.getName());//Object Prototype
- Array/Array.prototype 与 Function.prototype/Object.prototype
Array 是函数对象, 受 Function.prototype 的影响, 而 Array.prototype 不是函数对象, 所不受 Function.prototype 的影响, 但所有对象受 Object.prototype 的影响, 所以 Array.prototype 也会受 Object.prototype 的影响. 如:
- 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
来源: http://www.jianshu.com/p/211b8821e409