Skip to content
Browse files

Enhance isPlainObject function. fix #4

  • Loading branch information...
1 parent d4d04e9 commit 171984a9a7f6618001c2997988e17ddee8d916f3 @lifesinger lifesinger committed Sep 18, 2012
Showing with 31 additions and 7 deletions.
  1. +31 −7 src/attribute.js
View
38 src/attribute.js
@@ -142,6 +142,7 @@ define(function(require, exports) {
// -------
var toString = Object.prototype.toString;
+ var hasOwn = Object.prototype.hasOwnProperty;
var isArray = Array.isArray || function(val) {
return toString.call(val) === '[object Array]';
@@ -155,14 +156,37 @@ define(function(require, exports) {
return toString.call(val) === '[object Function]';
}
+ function isWindow(o) {
+ return o != null && o == o.window;
+ }
+
function isPlainObject(o) {
- return o &&
- // 排除 boolean/string/number/function 等
- // 标准浏览器下,排除 window 等非 JS 对象
- // 注:ie8- 下,toString.call(window 等对象) 返回 '[object Object]'
- toString.call(o) === '[object Object]' &&
- // ie8- 下,排除 window 等非 JS 对象
- ('isPrototypeOf' in o);
+ // Must be an Object.
+ // Because of IE, we also have to check the presence of the constructor
+ // property. Make sure that DOM nodes and window objects don't
+ // pass through, as well
+ if (!o || toString.call(o) !== "[object Object]" ||
+ o.nodeType || isWindow(o)) {
+ return false;
+ }
+
+ try {
+ // Not own constructor property must be Object
+ if (o.constructor &&
+ !hasOwn.call(o, "constructor") &&
+ !hasOwn.call(o.constructor.prototype, "isPrototypeOf")) {
+ return false;
+ }
+ } catch (e) {
+ // IE8,9 Will throw exceptions on certain host objects #9897
+ return false;
+ }
+
+ // Own properties are enumerated firstly, so to speed up,
+ // if last one is own, then all properties are own.
+ for (var key in o) {}
+
+ return key === undefined || hasOwn.call(o, key);
}
function isEmptyObject(o) {

0 comments on commit 171984a

Please sign in to comment.
Something went wrong with that request. Please try again.