SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Member
    Join Date
    Mar 2009
    Location
    Zagreb, Croatia
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Why is there an global property called undefined?

    So, among others these are the types in JavaScript:

    • the Null type which consists of only one value - null
    • the Boolean type which consists of the values true and false
    • the Undefined type which consists of only one value - undefined


    Of these four values (null, true, false, undefined), three of them are reserved words (null, true and false), but the last value (undefined) is not a reserved word.... It's an identifier. More precisely, it's a property of the Global object (which is the window object if we execute inside a browser).

    Why is that?

    It is a problem, because being a property, you can change its initial value (which is the undefined value of course) to another value like so...

    undefined = "hello world";

    If we do that, we risk our code to fail...

    if (someObject.property1 !== undefined) {
    someObject.propety1("blah");
    }

    In the above code, we are comparing the property with the string "hello world" which is not our intention...

    If we want to restore the original value of the undefined property, we have to do something like this...

    var x;
    undefined = x;

    If undefined would have been just a keyword like null, true and false, there would not be such issues... Why is it not just a keyword?
    What is the benefit of it being a global property?


    --
    Šime Vidas
    http://www.w3viewer.com

  2. #2
    SitePoint Guru
    Join Date
    Apr 2006
    Posts
    802
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Never mind...

  3. #3
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,840
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    undefined is NOT a property of the window object unless you create such a property. If you do create such a property then it will make the actual field inaccessible in those circumstances where the one you created is referenced. The actual undefined field will still be used in creating variables where a value is not assigned.

    If this were not the case then

    undefined = 'hello world;
    var x;

    would set x to 'hello world'.

    I don't know why undefined is not a reserved word but suspect that there is probably some obscure situation where it makes sense to be able to override it within a particular scope (probably not global scope) and that would not be possible if it were a reserved word.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  4. #4
    SitePoint Member
    Join Date
    Mar 2009
    Location
    Zagreb, Croatia
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm just reading the ECMAScript specification here (Edition 3) and it states...

    15.1 The Global object.

    ...

    15.1.1 Value Properties of the Global Object

    ...

    15.1.1.3 undefined

    The initial value of undefined is undefined. This property has the attributes { DontEnum, DontDelete }.


    Also, here...
    https://developer.mozilla.org/en/Cor...ties/undefined

    It said that undefined is a property of the global object.

  5. #5
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,840
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    As a property of the global object you can't actually give it a new value from within a script running in a browser since if you assign a value to undefined in a browser it will assign the value to window.undefined and will not touch the global version.

    The window object is not the same as the global object. There are only a few dozen or so properties and methods specific to the global object and to the best of my knowledge none of them can have their values in the global object changed from JavaScript running in a web page since any attempt to do so will just create a new version in the window object.

    Examples of things that belong to the global object rather than the window object include:

    isNaN()
    parseInt()
    encodeUri()
    eval()
    Object
    Function
    Number

    Changing the definition of any global property would break JavaScript - changing undefined would do relatively little harm compared to redefining Number or Function
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">


Tags for this Thread

Bookmarks

Posting Permissions

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