最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - IsPlainObject, thing? - Stack Overflow

programmeradmin1浏览0评论

Does this fn:

function isplainobj ( obj ) {
    return Object.prototype.toString.call( obj ) === "[object Object]";
}

do the same thing as jQuery's: $.isPlainObject() ?

Does this fn:

function isplainobj ( obj ) {
    return Object.prototype.toString.call( obj ) === "[object Object]";
}

do the same thing as jQuery's: $.isPlainObject() ?

Share Improve this question asked Aug 30, 2013 at 11:18 public overridepublic override 9828 silver badges17 bronze badges 2
  • This should work under certain circumstances. Please show me your snippet where you are using this function. – BVdjV Commented Aug 30, 2013 at 11:35
  • im just using it in a general javascript utitlity plugin im working on... – public override Commented Aug 30, 2013 at 11:58
Add a ment  | 

5 Answers 5

Reset to default 2

No, it doesn't.

Here is it's implementation:

isPlainObject: function( obj ) {
    var key;

    // 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 ( !obj || jQuery.type(obj) !== "object" || obj.nodeType || jQuery.isWindow( obj ) ) {
        return false;
    }

    try {
        // Not own constructor property must be Object
        if ( obj.constructor &&
            !core_hasOwn.call(obj, "constructor") &&
            !core_hasOwn.call(obj.constructor.prototype, "isPrototypeOf") ) {
            return false;
        }
    } catch ( e ) {
        // IE8,9 Will throw exceptions on certain host objects #9897
        return false;
    }

    // Support: IE<9
    // Handle iteration over inherited properties before own properties.
    if ( jQuery.support.ownLast ) {
        for ( key in obj ) {
            return core_hasOwn.call( obj, key );
        }
    }

    // Own properties are enumerated firstly, so to speed up,
    // if last one is own, then all properties are own.
    for ( key in obj ) {}

    return key === undefined || core_hasOwn.call( obj, key );
},

It checks if it's type is object, if it has or not a constructor and if its properties are own properties.

For example with your function an instance of a class will return true but in this case, because it has a constructor it will return false.

According to jQuery source code here http://james.padolsey./jquery/#v=1.10.2&fn=jQuery.isPlainObject, I have gone through all possibilities handled in jQuery's plain Object. The function you have mentioned is passing all the tests. And both performing in same way. Please check the below testcases

isplainobj({});
true

isplainobj(function(){});
false

isplainobj(document);
false

isplainobj(window);
false

isplainobj($);
false

isplainobj({});
true

isplainobj(isplainobj);
false

isplainobj({});
true

As per my understanding the object which has all own properties is a plain Object. Please correct me if I am wrong.

Edit:

According to below lines in jQuery

// Not own constructor property must be Object
    if (obj.constructor && !core_hasOwn.call(obj, "constructor") &&   !core_hasOwn.call(obj.constructor.prototype, "isPrototypeOf")) {
        return false;
    }

Object should not have own property constructor and obj.constructor.prototype should not have isPrototypeOf to ensure it is not in prototype chain. Please have a look at https://developer.mozilla/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/isPrototypeOf for more info.

This simple test should do it

  obj!=null && typeof(obj)=="object" && Object.getPrototypeOf(obj)==Object.prototype 

//to ensure Object.getPrototypeOf

 Object.getPrototypeOf||(Object.getPrototypeOf=function(obj){
    return obj.__proto__ || obj.prototype || (obj.constructor&&obj.constructor.prototype) || Object.prototype
});

From the jQuery source:

isPlainObject: function( obj ) {
    var key;

    // 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 ( !obj || jQuery.type(obj) !== "object" || obj.nodeType || jQuery.isWindow( obj ) ) {
        return false;
    }

    try {
        // Not own constructor property must be Object
        if ( obj.constructor &&
            !core_hasOwn.call(obj, "constructor") &&
            !core_hasOwn.call(obj.constructor.prototype, "isPrototypeOf") ) {
            return false;
        }
    } catch ( e ) {
        // IE8,9 Will throw exceptions on certain host objects #9897
        return false;
    }

    // Support: IE<9
    // Handle iteration over inherited properties before own properties.
    if ( jQuery.support.ownLast ) {
        for ( key in obj ) {
            return core_hasOwn.call( obj, key );
        }
    }

    // Own properties are enumerated firstly, so to speed up,
    // if last one is own, then all properties are own.
    for ( key in obj ) {}

    return key === undefined || core_hasOwn.call( obj, key );
},

const isPlainObject = obj => (obj?.constructor === Object || Object.getPrototypeOf(obj ?? 0) === null);

console.log(isPlainObject({})); //true
console.log(isPlainObject(Object.create(null))); //true
console.log(isPlainObject({...document.body})); //true
console.log(isPlainObject(window)); //false
console.log(isPlainObject(window._UNDEFINED)); //false
console.log(isPlainObject([1,2,3])); //false
console.log(isPlainObject(new Date)); //false

发布评论

评论列表(0)

  1. 暂无评论