I read a book on JavaScript'у , there is such an example:
Object.prototype.keys = function() { var keys = []; for(var p in this) keys.push(p); return keys; } var obj = {a: 1, b: 2, c: 3}; if(obj.keys().length == 3) { alert(1); } else { alert(2) } The bottom line is that the length property does not work correctly for some reason and I cannot figure out for what reason.
The following is an example where the check is added:
Object.prototype.keys = function() { var keys = []; for(var p in this) if (this.hasOwnProperty(p)) keys.push(p); return keys; } var obj = {a: 1, b: 2, c: 3}; if(obj.keys().length == 3) { alert(1); } else { alert(2) } I can not understand how this test affected the result of the implementation.
In my understanding, in the second example, it checks whether the property is inherited from an object or not, and if not, then it is added to the array with the results, and if so, it is not added. But in the test object there are no inherited properties, in fact the result is the same should come out, but it is not. Explain, please.
console.log(p)into the loop and see for yourself - andreymal