任何人能解释一下叫什么hasOwnProperty对空对象呢?为什么要使用它?你如何使用hasOwnProperty?
__hasProp = {}.hasOwnProperty
我发现这个时候,我开始在coffescript开发..泰
任何人能解释一下叫什么hasOwnProperty对空对象呢?为什么要使用它?你如何使用hasOwnProperty?
__hasProp = {}.hasOwnProperty
我发现这个时候,我开始在coffescript开发..泰
hasOwnProperty
是不调用。函数名称后没有括号。
__hasProp
为参考到Object.prototype.hasOwnProperty
[MDN]因为
__hasProp = {}.hasOwnProperty
是
__hasProp = Object.prototype.hasOwnProperty
较短版本现在调用__hasProp
,例如在
__hasProp.call(someObject, 'foo')
是米UCH更短,更易于阅读比
Object.prototype.hasOwnProperty.call(someObject, 'foo')
我公司成立时,我开始coffescript的发展。
所以,我认为是自动生成的这条线,你可能会奇怪为什么创建一个快捷方式,如果没有人实际上有写代码?
有了这个功能分配给一个变量避免了查找你想用的时候就Object
,Object.prototype
和Object.prototype.hasOwnProperty
。 __hasProp
只是一个查询,而不是三个,并且经常使用时,可能会导致轻微的性能提升。
了解,+1 thx :) – 2013-02-22 22:41:54
没有咖啡解析器将它添加到生成的代码?看起来它只是做一个简短的参考。 – 2013-02-22 21:22:46
'hasOwnProperty'是标准的JavaScript。 https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Object/hasOwnProperty – Marc 2013-02-22 21:23:53
我觉得这是一个大的离奇,但它的作用是基本对象的'hasOwnProperty'方法复制到一个命名变量'__hasProp'。也许这是因为名字较短? – 2013-02-22 21:24:08