How to understand if (name in {}) in javascript?

I came across a js function snippet as follows

each = function(obj, fun) { if (typeof fun != "function") { return obj } if (obj) { var return_value; if (obj.length === undefined) { for (var name in obj) { if (name in {}) { // how to undertand this line, what purpose? continue } return_value = fun.call(obj[name], obj[name], name); if (return_value == "break") { break } } } else { for (var i = 0, length = obj.length; i < length; i++) { return_value = fun.call(obj[i], obj[i], i); if (return_value == "break") { break } } } } return obj }; 

Thanks for your reply:)

+4
source share
3 answers

What he does is see if this property name exists in an empty object. So, for example, it will filter out toString (for implementations that are not errors regarding toString and in ; IE is).

It could be an attempt to get around people adding things to Object.prototype (this is a very, very bad idea). For instance:

 Object.prototype.foo = "bar"; // <== VERY BAD IDEA alert({}.foo); // alerts "bar" function Thingy(a, b) { this.a = a; this.b = b; } Thingy.prototype.x = 42; var t = new Thingy(1, 2); 

If I want to copy t , including its inherited property x from Thingy , but not including the foo property, which it inherits from Object , I could use this if (name in {}) to skip them.

Keep in mind that this is not a very smart way to do this, as it will not work if I do this:

 var t = new Thingy(1, 2); t.foo = "charlie"; 

Now t has its own foo , which I supposedly would like to copy.

More thorough check:

 dest = {}; blank = {}; // No need to recreate it on every iteration for (name in src) { if (src.hasOwnProperty(name) || !(name in blank)) { dest[name] = src[name]; } } 

This will filter out only those that are inherited, or at least can be inherited, from Object . But even that is spoiled; What if it inherits the value for the property, but not the value from Object (for example, something in the middle - Thingy possible - overrides Object by default)?

So you might want to reorganize this code that you inherited, perhaps in terms of using hasOwnProperty and make sure that nothing in your application puts things in Object.prototype .

+4
source

In is a special operator that returns true if the specified property is in the specified object.

From https://developer.mozilla.org/en/JavaScript/Reference/Operators/Special_Operators/in_Operator

Each object, including {}, inherits properties from Object.prototype. if here makes the loop continue if name is one of those inherited properties.

Example: console.log('toString' in {}); logs true . console.log('customProperty' in {}); logs false.

+1
source

The in operator returns true if the second operand has a property with the identifier of the first operand, and false otherwise. So, name in {} checks to see if there is a property with a value name .

This probably seems strange, since {} does not seem to have any properties. But a simple object inherits some properties of the Object.prototype object. So, "toString" in {} returns true, since there is Object.prototype.toString . There is also a hasOwnProperty method, inherited from Object.prototype.hasOwnProperty , which can be used instead.

+1
source

Source: https://habr.com/ru/post/1333036/


All Articles