使用原型来表现得像“setter function”

时间:2013-10-03 07:17:17

标签: javascript

我使用了类MC并添加了像这样的“setLabel”函数

MC.prototype.setLabel = function( value )
{

   this.labelTextfield.text = value  
} 

Mc.prototype.setLabel( "some random text" ) ;

但由于某些原因,我希望能够做到这样的事情:

Mc.prototype.label = "some random text" 

我认为,如果我可以在原型中使用setter函数,那么这可能是可能的。但我不确定,如何使用原型的setter函数?任何帮助表示赞赏。

Vishwas

1 个答案:

答案 0 :(得分:1)

Object的defineProperty()方法可能是您最好的选择,但我认为您应该使用“严格”模式。

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/defineProperty