2

我试图使该方法getColor()不可配置,但是当我尝试使用this.color它来引用 Fruit“类”的当前实例属性时,它不起作用。好像this没有引用Fruit实例。如何引用实例?

为属性定义的函数体不是突变体,这里只是一个例子

Object.defineProperty(Fruit.prototype, 'getColor', {
    writable: false,
    configurable: false,
    enumerable: false,
    value: function(){
        return this.color;  // this refers to Object here
    }
});
4

0 回答 0