www.webdeveloper.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 16

Thread: Determining if a javascript object is empty?

  1. #1
    Join Date
    Jun 2008
    Posts
    20

    Determining if a javascript object is empty?

    var someObj = {}

    Is there something I use to determine that someObj is empty? It's still a typeof object but has no attributes.

  2. #2
    Join Date
    Aug 2007
    Posts
    3,767
    Well what will happen is that a for in loop won't execute with an empty object. I don't know of any other way to check.

    What is the exact problem? There might be a better way to code it to avoid having to know.

  3. #3
    Join Date
    Jul 2008
    Location
    urbana, il
    Posts
    2,787
    Code:
    function isEmpty(ob){
       for(var i in ob){ return false;}
      return true;
    }
    
    isEmpty({a:1}) // false
    isEmpty({}) // true

  4. #4
    Join Date
    Oct 2008
    Posts
    2
    if (someObj = undefined){do this}

  5. #5
    Join Date
    Sep 2006
    Location
    Copenhagen, Denmark
    Posts
    1,253
    @ rnd_me: That will only work if the object has no enumerable prototype properties.

    Try this
    Code:
    function isEmpty(o) {
      var o = {};
      for(var p in o) {
        if (o[p] != o.constructor.prototype[p])
          return false;
      }
      return true;
    }

  6. #6
    Join Date
    Jun 2008
    Posts
    20
    I found a way mentioned by the guy who made JSON (forgot his name) on his website. Basically you use a for loop on the specified object and assign the first iterable item into a new variable. As you said before, if the object is empty, the for loop never runs, and therefore if that new variable you assigned the first element to is undefined the object is empty.

  7. #7
    Join Date
    Sep 2006
    Location
    Copenhagen, Denmark
    Posts
    1,253
    ...but still it will fail if the object from which it was constructed has properties defined in its prototype obejct. You need to apply the check in my previous post

  8. #8
    Join Date
    Mar 2005
    Location
    Sydney, Australia
    Posts
    7,974
    The problem is that a for-in loop will only loop through the enumerable properties. There is no way to tell if an object has or does not have properties that are not enumerable unless you test for them specifically by name. All objects will have at least two such properties.

    Where you have

    var someObj = {}

    both someObj.constructor and someObj.prototype will exist because all JavaScript objects have those two properties regardless of what other properties they may have. They will never show up in a loop though because they are not enumerable properties.

  9. #9
    Join Date
    Jul 2008
    Location
    urbana, il
    Posts
    2,787
    Quote Originally Posted by Dok View Post
    ...but still it will fail if the object from which it was constructed has properties defined in its prototype obejct. You need to apply the check in my previous post
    i didn't think about that since the example was a simple primitive anon.

    but its a valid point, so here is a better version:
    Code:
    function isEmpty(ob){
       for(var i in ob){ if(ob.hasOwnProperty(i)){return false;}}
      return true;
    }

    EDIT: whoops, didn't see you had posted a solution. didn't mean to 2nd guess you Dok...
    Last edited by rnd me; 10-16-2008 at 06:05 PM.

  10. #10
    Join Date
    Jul 2008
    Location
    urbana, il
    Posts
    2,787
    Quote Originally Posted by felgall View Post
    var someObj = {}
    both someObj.constructor and someObj.prototype will exist because all JavaScript objects have those two properties regardless of what other properties they may have.

    where is prototype of anon objects ?
    Code:
    var someObj = {}
    typeof someObj.prototype // "undefined"
    
    // more so:
    var someObj = {a:1}
    !! "prototype" in someObj //false
    !! "constructor" in someObj //false
    "a" in someObj //true
    
    // furthermore:
    someObj.hasOwnProperty("a") //true
    someObj.hasOwnProperty("prototype") //false
    someObj.hasOwnProperty("constructor") //false

    do even primitive objects have constructor and prototype properties?
    if so, can you please demonstrate how to find these properties?

    i can't find them using any the the three property detections posted above.
    Last edited by rnd me; 10-16-2008 at 06:15 PM.

  11. #11
    Join Date
    Mar 2005
    Location
    Sydney, Australia
    Posts
    7,974
    All anon objects are decendents of the built in Object object and thus inherit its properties.

  12. #12
    Join Date
    Jul 2008
    Location
    urbana, il
    Posts
    2,787
    Quote Originally Posted by felgall View Post
    All anon objects are decendents of the built in Object object and thus inherit its properties.
    that's what i thought, but i can't seem to find them. is it possible that prototype is there, but set to undefined?

    i thought an empty object was used, but again i can't find it and that doesn't appear to be the case:
    Code:
    var b="some var";
    b.prototype.someProp = 1
    // throws: TypeError: b.prototype is undefined
    if it were an empty object, it should accept properties right?

    so my question is what is b.prototype in the example above?


    EDIT:
    never mind. it is undefined.
    the code i posted should do what was asked of it.
    Last edited by rnd me; 10-16-2008 at 10:15 PM.

  13. #13
    Join Date
    Sep 2006
    Location
    Copenhagen, Denmark
    Posts
    1,253
    @ rnd_me:
    Code:
    Object.prototype.p = 'Hello';
    var o = {};
    alert(o.p);  // Output 'Hello'
    
    o.prototype  // Undefined
    o.constructor.prototype  // The prototype object of Object
    o.constructor.prototype.isPrototypeOf(Object)  // True
    I don't know why the prototype object of the instance o is not available as o.prototype...
    Last edited by Dok; 10-17-2008 at 08:21 AM.

  14. #14
    Join Date
    Jul 2008
    Location
    urbana, il
    Posts
    2,787
    Quote Originally Posted by Dok View Post
    @ rnd_me:
    Code:
    Object.prototype.p = 'Hello';
    var o = {};
    alert(o.p);  // Output 'Hello'
    
    o.prototype  // Undefined
    o.constructor.prototype  // The prototype object of Object
    o.constructor.prototype.isPrototypeOf(Object)  // True
    I don't know why the prototype object of the instance o is not available as o.prototype...
    it weird. i thought primitives had an empty version of themselves as prototype.

    Code:
    s= "string"
    s.prototype // undefined
    s.bold //bold()
    the props of the proto are there, but the proto itself seems not to be.
    it one of those little things nobody notices;

    one answer:
    from the spec: (15.2.3.1) The initial value of Object.prototype is the Object prototype object (section 15.2.4).

    if you have not bound anything to Object.prototype, this should be it's default.

    from the spec: (15.2.4) The value of the internal [[Prototype]] property of the Object prototype object is null

    so i was wrong. it's null, not undefined.

    that being said, i don't know how it came up, and i don't see the relevance to the OP's question.

  15. #15
    Join Date
    Dec 2003
    Location
    Bucharest, ROMANIA
    Posts
    15,428
    What about?:
    Code:
    var obj={}
    Object.prototype.isEmpty = function() {
        for (var prop in this) {
            if (this.hasOwnProperty(prop)) return false;
        }
        return true;
    };
    alert(obj.isEmpty())

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles