Skip to content

Instantly share code, notes, and snippets.

@lizzie
Last active December 18, 2015 14:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lizzie/5795030 to your computer and use it in GitHub Desktop.
Save lizzie/5795030 to your computer and use it in GitHub Desktop.
判断是否空对象
function isEmptyObject(o) { // jQuery
var name;
for (name in o) {
return false;
}
return true;
}
function isEmptyObject(o) { // KISSY
for (var p in o) {
if (p !== undefined) {
return false;
}
}
return true;
}
// arale 的, 含基本类型... 所以要先 if 判断
// 要排除 isEmptyObject(1) 这种情况, 所以不能用上面的两段
function isEmptyObject(o) {
if (!o || Object.prototype.toString.call(o) !== "[object Object]" ||
o.nodeType || isWindow(o) || !o.hasOwnProperty) {
return false;
}
for (var p in o) {
if (o.hasOwnProperty(p)) return false;
}
return true;
}
for(var i in location){console.log(Object.prototype.hasOwnProperty.call(location, i));}
for(var i in location){console.log(location.hasOwnProperty(i));}
// 两者结果不一样
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment