Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. JavaScript
  4. 'undefined' is a flaw in JavaScript

'undefined' is a flaw in JavaScript

Scheduled Pinned Locked Moved JavaScript
javascripthelp
7 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    le_top
    wrote on last edited by
    #1

    I now consider that the way that 'undefined' is implemented in JavaScript represents a flaw in the language. IMHO, everything (at least variables) in JavaScript are Objects, but I just realize that variables that are 'undefined', are not. It is unfortunate that constant values are not objects. It would be a nice to have. For instance, it is possible to do:

    var a=1; a.toString();

    and not

    1.toString();

    . With regards to undefined, it isn't possible to do neither

    var a=undefined; a.toString();

    nor

    undefined.toString();

    Hence it breaks several principles that Programming Languages should respect. If 'undefined' were an object, it could act as a Null Object, and all variables would automatically have a Null Object. It would then suffice to do something like:

    Object.prototype.isMyObject=false;
    MyObject.prototype.isMyObject=true;

    and then one could always write:

    if(anyvar.isMyObject){ /*...*/ }

    without falling in the undefined issue. However, I do understand that today many developers may partly rely on 'undefined' resulting in an exception to detect flaws in their programs. Therefore a new option might be added to JavaScript, much like "strict" in order to indicate that 'undefined' should act as a Null Object. But that might not even be necessary, as the exception could simply be thrown only when trying to get a missing member of the object, as in:

    undefined.missingVariable

    or

    undefined.missingFunction()

    . So in conclusion: having undefined variables behave as Objects would make the JavaScript language "more complete", and having constants (like the number 1) behave as Objects would take it even one step further. If those impacting the definition of the language could consider that, I would be quite happy about it.

    -- Mario DW

    Kornfeld Eliyahu PeterK 1 Reply Last reply
    0
    • L le_top

      I now consider that the way that 'undefined' is implemented in JavaScript represents a flaw in the language. IMHO, everything (at least variables) in JavaScript are Objects, but I just realize that variables that are 'undefined', are not. It is unfortunate that constant values are not objects. It would be a nice to have. For instance, it is possible to do:

      var a=1; a.toString();

      and not

      1.toString();

      . With regards to undefined, it isn't possible to do neither

      var a=undefined; a.toString();

      nor

      undefined.toString();

      Hence it breaks several principles that Programming Languages should respect. If 'undefined' were an object, it could act as a Null Object, and all variables would automatically have a Null Object. It would then suffice to do something like:

      Object.prototype.isMyObject=false;
      MyObject.prototype.isMyObject=true;

      and then one could always write:

      if(anyvar.isMyObject){ /*...*/ }

      without falling in the undefined issue. However, I do understand that today many developers may partly rely on 'undefined' resulting in an exception to detect flaws in their programs. Therefore a new option might be added to JavaScript, much like "strict" in order to indicate that 'undefined' should act as a Null Object. But that might not even be necessary, as the exception could simply be thrown only when trying to get a missing member of the object, as in:

      undefined.missingVariable

      or

      undefined.missingFunction()

      . So in conclusion: having undefined variables behave as Objects would make the JavaScript language "more complete", and having constants (like the number 1) behave as Objects would take it even one step further. If those impacting the definition of the language could consider that, I would be quite happy about it.

      -- Mario DW

      Kornfeld Eliyahu PeterK Offline
      Kornfeld Eliyahu PeterK Offline
      Kornfeld Eliyahu Peter
      wrote on last edited by
      #2

      First of all - numbers and strings are not object in JavaScript, but just like undefined are 'primitive types'...The difference is that for string and number there is a wrapping object but not for undefined...You are welcome to create one (and resolve your problem undefined not being an object)!!! As for the 1.toString() part...It is not hing more than a parsing problem...In JavaScript there is only number, no int or float or other sub-types... So 1.t is a syntax problem only - try 1.0.toString() and see how it works (so constant values are objects, when it suits the parser/compiler)...

      Skipper: We'll fix it. Alex: Fix it? How you gonna fix this? Skipper: Grit, spit and a whole lotta duct tape.

      "It never ceases to amaze me that a spacecraft launched in 1977 can be fixed remotely from Earth." ― Brian Cox

      L C 2 Replies Last reply
      0
      • Kornfeld Eliyahu PeterK Kornfeld Eliyahu Peter

        First of all - numbers and strings are not object in JavaScript, but just like undefined are 'primitive types'...The difference is that for string and number there is a wrapping object but not for undefined...You are welcome to create one (and resolve your problem undefined not being an object)!!! As for the 1.toString() part...It is not hing more than a parsing problem...In JavaScript there is only number, no int or float or other sub-types... So 1.t is a syntax problem only - try 1.0.toString() and see how it works (so constant values are objects, when it suits the parser/compiler)...

        Skipper: We'll fix it. Alex: Fix it? How you gonna fix this? Skipper: Grit, spit and a whole lotta duct tape.

        L Offline
        L Offline
        le_top
        wrote on last edited by
        #3

        Peter, thanks for the interesting feedback. Thanks to your keywords I found this: ECMAScript Language Specification paragraph 7.1.13 on page number 43 indicates that 'undefined' and 'null' are not converted into an object. So yes, you are right that numbers and strings are not objects, but they are "coerced" (if that is the right word) into objects temporarily when the context kind of asks for it. It would be nice that that also applies to 'undefined' and 'null'. I do not see a way to define the "automatic" and "temporary" conversion to an object myself, just the way it happens for strings, numbers and booleans. If you have a way, I'll take it ;-).

        Kornfeld Eliyahu PeterK 1 Reply Last reply
        0
        • L le_top

          Peter, thanks for the interesting feedback. Thanks to your keywords I found this: ECMAScript Language Specification paragraph 7.1.13 on page number 43 indicates that 'undefined' and 'null' are not converted into an object. So yes, you are right that numbers and strings are not objects, but they are "coerced" (if that is the right word) into objects temporarily when the context kind of asks for it. It would be nice that that also applies to 'undefined' and 'null'. I do not see a way to define the "automatic" and "temporary" conversion to an object myself, just the way it happens for strings, numbers and booleans. If you have a way, I'll take it ;-).

          Kornfeld Eliyahu PeterK Offline
          Kornfeld Eliyahu PeterK Offline
          Kornfeld Eliyahu Peter
          wrote on last edited by
          #4

          Think about how JavaScript enclosed inside DOM... When you write var n = new Number();, Number is a shortcut for window.Number. Same goes for undefined (it is a property of the window object, but not number or string)...The problem is that since ECMA 5 it is read-only and for that can not be overridden... So there is no way to 'fake' and automatic boxing of undefined in the context of window, but you can create an object that does it and create your own closure (scope) that will define undefined as that object... If you ever saw an opening line like this...

          (function (window, document) {
          // we refer to window and document normally
          })(window, document);

          Didn't you asked yourself why those parameters, especially undefined? So it is because before ECMA 5 undefined was mutable... Now if you do not bring undefined in (to protect yourself) into your closure, you can define an the name 'undefined' with other value and start using it inside your closure...

          Skipper: We'll fix it. Alex: Fix it? How you gonna fix this? Skipper: Grit, spit and a whole lotta duct tape.

          "It never ceases to amaze me that a spacecraft launched in 1977 can be fixed remotely from Earth." ― Brian Cox

          L 1 Reply Last reply
          0
          • Kornfeld Eliyahu PeterK Kornfeld Eliyahu Peter

            Think about how JavaScript enclosed inside DOM... When you write var n = new Number();, Number is a shortcut for window.Number. Same goes for undefined (it is a property of the window object, but not number or string)...The problem is that since ECMA 5 it is read-only and for that can not be overridden... So there is no way to 'fake' and automatic boxing of undefined in the context of window, but you can create an object that does it and create your own closure (scope) that will define undefined as that object... If you ever saw an opening line like this...

            (function (window, document) {
            // we refer to window and document normally
            })(window, document);

            Didn't you asked yourself why those parameters, especially undefined? So it is because before ECMA 5 undefined was mutable... Now if you do not bring undefined in (to protect yourself) into your closure, you can define an the name 'undefined' with other value and start using it inside your closure...

            Skipper: We'll fix it. Alex: Fix it? How you gonna fix this? Skipper: Grit, spit and a whole lotta duct tape.

            L Offline
            L Offline
            le_top
            wrote on last edited by
            #5

            I've tried the following, and 'undefined' does not seem 'fakable' as you say. A local variable can assume another value, but the value of that variable is of course not assumes by undefined variables.

            (function(window){
            var undefined='a';
            console.log(undefined); // prints 'a'
            console.log(window); // prints 'undefined' -> window is defined as 'undefined'
            console.log(a); // throws error (a is undefined).
            })(undefined)

            The following code also changes the local value of 'undefined', but not the 'undefined' of 'a' itself.

            (function(undefined){
            console.log(undefined); // prints 'a'
            console.log(a); // throws error (a is undefined).
            })('a')

            Kornfeld Eliyahu PeterK 1 Reply Last reply
            0
            • L le_top

              I've tried the following, and 'undefined' does not seem 'fakable' as you say. A local variable can assume another value, but the value of that variable is of course not assumes by undefined variables.

              (function(window){
              var undefined='a';
              console.log(undefined); // prints 'a'
              console.log(window); // prints 'undefined' -> window is defined as 'undefined'
              console.log(a); // throws error (a is undefined).
              })(undefined)

              The following code also changes the local value of 'undefined', but not the 'undefined' of 'a' itself.

              (function(undefined){
              console.log(undefined); // prints 'a'
              console.log(a); // throws error (a is undefined).
              })('a')

              Kornfeld Eliyahu PeterK Offline
              Kornfeld Eliyahu PeterK Offline
              Kornfeld Eliyahu Peter
              wrote on last edited by
              #6

              It just shows how fundamentally 'undefined' is deep in the JavaScript engine... As you (we) can not change it from the outside, you may talk to the committee...

              Skipper: We'll fix it. Alex: Fix it? How you gonna fix this? Skipper: Grit, spit and a whole lotta duct tape.

              "It never ceases to amaze me that a spacecraft launched in 1977 can be fixed remotely from Earth." ― Brian Cox

              1 Reply Last reply
              0
              • Kornfeld Eliyahu PeterK Kornfeld Eliyahu Peter

                First of all - numbers and strings are not object in JavaScript, but just like undefined are 'primitive types'...The difference is that for string and number there is a wrapping object but not for undefined...You are welcome to create one (and resolve your problem undefined not being an object)!!! As for the 1.toString() part...It is not hing more than a parsing problem...In JavaScript there is only number, no int or float or other sub-types... So 1.t is a syntax problem only - try 1.0.toString() and see how it works (so constant values are objects, when it suits the parser/compiler)...

                Skipper: We'll fix it. Alex: Fix it? How you gonna fix this? Skipper: Grit, spit and a whole lotta duct tape.

                C Offline
                C Offline
                camycentsolutions
                wrote on last edited by
                #7

                Instead of 1.toString () we can use (1).toString().It will work fine by the java script perser.

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups