javascript - Why does the hasOwnProperty alias falsely return undefined? -
function object() { var f = function() {}; f.prototype = { alias: {}, hasownproperty: function() { return false; }, hasproperty: function(obj, prop) { (var = 0; < obj.length; i++) { if (obj[i] !== prop) return false; else if (obj[i] === prop) return true; else return undefined; } } }; return new f(); } var newobj = object(); newobj.alias.msg = "hello"; console.log(newobj.hasproperty(newobj.alias, "hello")); it returns undefined newobj.hasproperty(newobj.alias, "hello"). why?
well, 1 thing, alias plain object, , therefore doesn't have .length property can used for loop.
if place:
console.log(i); ...in for loop, you'll notice never run block.
and if fixed loop, you're still doing return in first enumeration, if property you're testing isn't first in loop, you'll incorrect result.
ultimately, won't able replicate behavior of hasownproperty() loop, because prototype included in enumeration.
Comments
Post a Comment