2

i've tried to do a very simple IIFE below,

<script type="text/javascript">

    var obj = new Object;

    (function(_obj) {_obj.prop = 'defined';})(obj);

    if(typeof obj.prop === undefined)
     alert('undefined');
    else
     alert(obj.prop);

</script>

Why does the script alert "undefined" than "defined" as a result?

@EDIT

The script shoud have worked as expected except:

  • The unintended typeof obj.prop === undefined is wrong, but obj.prop === undefined should be used instead.
  • When omitting parenthesis below, the script doesn't work as expected but none of syntax error is raised from rhino.

    function(_obj) {_obj.prop = 'defined';}(obj);

sof
  • 9,113
  • 16
  • 57
  • 83
  • 1
    Although your comparison is wrong (`typeof obj.prop` returns a string, so `typeof obj.prop === undefined` will **always** be `false` and I don't think you intended to do this), it alerts `defined`. Please create http://jsfiddle.net/ to replicate your problem, the posted code does not show the behaviour you say it does, so we cannot help you. – Felix Kling Feb 26 '12 at 20:10
  • More people probably run into the `typeof obj.prop === undefined` bug than the redefining `undefined` bug that it's supposed to fix. –  Feb 26 '12 at 20:13
  • 1
    So is your question now why omitting the parenthesis doesn't work? If so, it's a duplicate of http://stackoverflow.com/q/670623/218196 – Felix Kling Feb 26 '12 at 20:21
  • Yes, `typeof obj.prop` returns a string. corrected. – sof Feb 26 '12 at 20:24
  • sof: Don't change your question. @FelixKling: I'm going to roll it back. –  Feb 26 '12 at 20:24
  • No, omitting the parenthesis doesn't work. – sof Feb 26 '12 at 20:24
  • @Felix Kling: Yes, it's duplicate. but none of syntax error is raised when omitting the parenthesis. – sof Feb 26 '12 at 20:31
  • I get one: *SyntaxError: Unexpected token (*. – Felix Kling Feb 26 '12 at 20:33

3 Answers3

3

Your code alerts 'defined' but for the wrong reason.

This...

typeof obj.prop === undefined

should be this...

obj.prop === undefined

...because typeof returns a string representing the type of object.


Don't use the typeof hack when testing for undefined. It's confusing, and can be the source of bugs such as the one you encountered.

If you're that worried about undefined being redefined, then do this...

obj.prop === void 0
  • Why is using `typeof` a hack? What's wrong with `typeof obj.prop === "undefined"`? – Felix Kling Feb 26 '12 at 20:15
  • @FelixKling It's a hack because it's a way of getting around a presumed problem rather than dealing with the problem directly *(when it actually exists)*. IMO, it's embarrassingly ugly code that isn't well representative of what is desired. Also, it's probably the source of more bugs like the one in the question than the bugs it's supposed to avoid. –  Feb 26 '12 at 20:21
  • @amnotiam Stuff like `typeof foo.bar !== 'function'` is fine though... The `typeof` operator has its applications. – Šime Vidas Feb 26 '12 at 20:35
1

Should be:

if ( typeof obj.prop === 'undefined' )

The typeof operator returns strings.

Šime Vidas
  • 182,163
  • 62
  • 281
  • 385
1

typeof returns a string. So, you need;

if(typeof obj.prop === "undefined")

The code seems to work as expected after you fix this issue: http://jsfiddle.net/jfriend00/KJBjG/

jfriend00
  • 683,504
  • 96
  • 985
  • 979
  • That's correct but does not explain the behaviour the OP is experiencing (I'm not implying that there is an explanation). – Felix Kling Feb 26 '12 at 20:13
  • @FelixKling - Once this issue is fixed, I can't reproduce the OP's results [here](http://jsfiddle.net/jfriend00/KJBjG/) and the code seems to work as expected. I would doubt they can either. – jfriend00 Feb 26 '12 at 20:16
  • Me too... I'm just talkative today ;) – Felix Kling Feb 26 '12 at 20:18