function func(){}
alert(func.prototype);//[object Object]
alert(func.__proto__);//function (){[native code]}
b=new func;
alert(b.__proto__);//[object Object]
func.prototype=Array;//继承后
alert(b.__proto__);//[object Object]
alert(func.prototype);//function Array(){[native code]}
alert(func.__proto__);//function (){[native code]}
a=new Array;
alert(a.__proto__);//空,为什么不是[object Object]???
alert(Array.prototype);//空,为什么不是function Object(){[native code]}???
Array.prototype=new Object;//继承后
alert(Array.prototype);//空??为什么???
Array或其他引用类型怎么继承Object的所有属性和方法???
Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
所有对象默认都已经继承了
Object的属性和方法。不要用
alert,空数组用alert显示不出来。在控制台输入代码查看结果。Array.prototype是原生的对象模型,若你想在原生对象的基础上扩展,就要使用到它
比如对数组增加contains方法:
通过原型链继承的。
这个需要看看数组原型和继承链。
然后自己多试试就理解了。
Array.prototype指的是原生对象的原型,所有的引用类型都默认继承了Object,这个继承是通过原型链实现的,默认原型都包含一个内部指针指向Object.prototype,会继承toString(),valueOf()等方法。