Skip to main content

JavaScript: Is a member defined?



It seems to me that there are four different ways I can determine whether a given object (e.g. foo ) has a given property (e.g. bar ) defined:







  1. if (foo.hasOwnProperty(bar)) {





  2. if ('bar' in foo) {





  3. if (typeof foo.bar !== 'undefined') {





  4. if (foo.bar === undefined) {







To determine if there is a property named " bar " in the object foo , are all three of those statements equivalent? Are there any sublte semantics I don't know that makes any of these three statements different?



Source: Tips4all

Comments

  1. No they are totally different. Example:

    foo = {bar: undefined};
    Object.prototype.baz = undefined;
    Object.prototype.bing = "hello";


    Then:

    (typeof foo.bar != "undefined") === false
    ('bar' in foo) === true
    (foo.hasOwnProperty('bar')) === true


    (typeof foo.baz != "undefined") === false
    ('baz' in foo) === true
    (foo.hasOwnProperty('baz')) === false


    (typeof foo.bing != "undefined") === true
    ('bing' in foo) === true
    (foo.hasOwnProperty('bing')) === false


    Logic-wise:


    foo.hasOwnProperty('bar') implies 'bar' in foo
    typeof foo.bar != "undefined" implies 'bar' in foo
    But those are the only inferences you can draw; no other implications are universally true, as the above counterexamples show.

    ReplyDelete
  2. one difference is that, method 1 will check only foo object for property bar while the last two methods will also check the prototype for inherited property.

    ReplyDelete
  3. There are indeed some subtle differences between the various methods/keywords.


    foo.hasOwnProperty('bar') returns true only if the property 'bar' is defined on the foo object itself. Other properties, such as 'toString' will return false however since they are defined up the prototype chain.
    The in keyword operator returns true if the specified property is in the specified object. Both 'bar' in foo and 'toString' in foo would return true.
    Since you are checking for the state of the property, the result will be true when bar is not defined on foo and when bar is defined but the value is set to undefined.

    ReplyDelete
  4. 'bar' in foo


    will look anywhere up the prototype chain. Testing to see if foo.bar !== undefined will also return true if bar is anywhere in foo's prototype chain, but remember if bar is defined on foo, and set to undefined, this will return false.

    hasOwnProperty is more choosy - it will only return true is bar is defined as a direct property of foo.

    Per MDN


    Every object descended from Object inherits the hasOwnProperty method.
    This method can be used to determine whether an object has the
    specified property as a direct property of that object; unlike the in
    operator, this method does not check down the object's prototype
    chain.

    ReplyDelete

Post a Comment

Popular posts from this blog

Why is this Javascript much *slower* than its jQuery equivalent?

I have a HTML list of about 500 items and a "filter" box above it. I started by using jQuery to filter the list when I typed a letter (timing code added later): $('#filter').keyup( function() { var jqStart = (new Date).getTime(); var search = $(this).val().toLowerCase(); var $list = $('ul.ablist > li'); $list.each( function() { if ( $(this).text().toLowerCase().indexOf(search) === -1 ) $(this).hide(); else $(this).show(); } ); console.log('Time: ' + ((new Date).getTime() - jqStart)); } ); However, there was a couple of seconds delay after typing each letter (particularly the first letter). So I thought it may be slightly quicker if I used plain Javascript (I read recently that jQuery's each function is particularly slow). Here's my JS equivalent: document.getElementById('filter').addEventListener( 'keyup', function () { var jsStart = (new Date).getTime()...

Is it possible to have IF statement in an Echo statement in PHP

Thanks in advance. I did look at the other questions/answers that were similar and didn't find exactly what I was looking for. I'm trying to do this, am I on the right path? echo " <div id='tabs-".$match."'> <textarea id='".$match."' name='".$match."'>". if ($COLUMN_NAME === $match) { echo $FIELD_WITH_COLUMN_NAME; } else { } ."</textarea> <script type='text/javascript'> CKEDITOR.replace( '".$match."' ); </script> </div>"; I am getting the following error message in the browser: Parse error: syntax error, unexpected T_IF Please let me know if this is the right way to go about nesting an IF statement inside an echo. Thank you.