We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
var man = { hands: 2, legs: 2, heads: 1 }; // 在代码的某个地方 // 一个方法添加给了所有对象 if (typeof Object.prototype.clone === "undefined") { Object.prototype.clone = function () {}; }
for (var i in man) { console.log(i, ":", man[i]); }
错误的,会把继承方法和熟悉打印出来;
for (var i in man) { if (man.hasOwnProperty(i)) { // 过滤 console.log(i, ":", man[i]); } }
使用对象上的 hasOwnProperty 方法判断;
var i, hasOwn = Object.prototype.hasOwnProperty; for (i in man) { if (hasOwn.call(man, i)) { // 过滤 console.log(i, ":", man[i]); } }
使用了 Object上的hasOwnProperty 方法判断,避免 man方法上hasOwnProperty 被重写而导致的错误;
The text was updated successfully, but these errors were encountered:
No branches or pull requests
情况1
改进
推荐
The text was updated successfully, but these errors were encountered: