how do you use hasOwnProperty?

class, coffeescript, javascript

Solution

`hasOwnProperty` is not called. There are no parenthesises after the function name.

`__hasProp` is a reference to `Object.prototype.hasOwnProperty` [MDN] because

__hasProp = {}.hasOwnProperty

is a shorter version of

__hasProp = Object.prototype.hasOwnProperty

Calling `__hasProp` now, for example in

__hasProp.call(someObject, 'foo')

is much shorter and easier to read than

Object.prototype.hasOwnProperty.call(someObject, 'foo')

I founded when I start to development of coffescript.

So I assume this line was automatically generated and you might wonder why create a shortcut if no one actually has to write the code?

Having this function assigned to a variable avoids having to lookup `Object`, `Object.prototype` and `Object.prototype.hasOwnProperty` every time you want to use it. `__hasProp` is just one lookup, instead of three, and might lead to a slight performance increase when used very often.

Problem

Can anybody explain what hasOwnProperty called on empty object does? Why use it? ``` __hasProp = {}.hasOwnProperty ``` I found this when I started to develop in coffescript.. Ty

Original source