簡體   English   中英

是否從對象繼承了defineProperty方法?

[英]Is defineProperty method inherited from Object?

如果所有對象都繼承自Object,那么為什么我不能以這種方式使用Object的defineProperty方法?

var car = {name:"honda"}

car.defineProperty(car, "jow", {value:"jow"})

alert(car.jow)

感謝您的見解,

因為defineProperty不是Object原型的屬性,所以它是Object本身的屬性。

因此,要使用它,請執行以下操作:(從文檔中https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Object/defineProperty

var o = {}; // Creates a new object

// Example of an object property added with defineProperty with a data property descriptor
Object.defineProperty(o, "a", {value : 37,
                               writable : true,
                               enumerable : true,
                               configurable : true});
// 'a' property exists in the o object and its value is 37

另外,這里有一個很好的鏈接解釋原型和繼承: http : //msdn.microsoft.com/zh-cn/magazine/ff852808.aspx

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM