0

In the code below, I added a new property to the function obj1. However, when I print the value of obj1.name, it shows nothing, just blank!

Well, if what I am doing is wrong then the JavaScript engine has to complain, but it does not. So what is going on here?

var obj1 = function (){
    name:"john";
};


console.log(obj1.name);// prints nothing!
C graphics
  • 7,308
  • 19
  • 83
  • 134

1 Answers1

1

name: token here is parsed as a label.

So it's a syntactically valid script that:

  1. declares a label
  2. evaluates a string expression

So, obj1.name is still undefined since you haven't set its properties anywhere.

References:

zerkms
  • 249,484
  • 69
  • 436
  • 539