Last active
December 18, 2015 14:09
-
-
Save lizzie/5795030 to your computer and use it in GitHub Desktop.
判断是否空对象
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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