在 ECMAScript 中,没有对象的 __proto__
:
Array.hasOwnProperty('prototype') //true
var arr = new Array()
arr.hasOwnProperty('__proto__') //false
然后,我们可以发现:
Object.getOwnPropertyDescriptors(arr)
输出:
length:{value: 1, writable: true, enumerable: false, configurable: false}
__proto__:Object
所以,我很困惑:
arr
是否有自己的属性 __proto__
?
当我尝试做以下事情时:
arr.unshift("2")
Js引擎在哪里找到unshift
方法?
有没有让Js引擎找到unshift
的信息?
最佳答案
Where does Js engine find unshift method?
在Array.prototype
上,arr
继承。每个对象上都有一个内部原型(prototype)链链接,通常称为[[prototype]]。您可以使用 Object.getPrototypeOf
访问它.
Object.getPrototypeOf(arr) == Array.prototype
So, I am confused: Does arr has his own property
__proto__
?
不,__proto__
是继承自Object.prototype
的setter/getter,它不是自己的 属性。 (不过,您可以在 arr 中找到 "__proto__"
)。和 it's deprecated .最好忘记它。
Why does
Object.getOwnPropertyDescriptors(arr)
output a__proto__
?
因为控制台使用这个名称来表示内部原型(prototype)链接。 getOwnPropertyDescriptors
返回一个对象,自然继承自Object.prototype
。没什么特别的。您也会在空对象 {}
中找到它。
关于javascript - Js中 "hasOwnProperty"和 "__proto__"怎么理解?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48984407/