0

I'm new here and new to Javascript. I got a strange problem here when I output the values to the console. As far as I know In both these cases the name, and the color is the properties of object car to access them we need to use this.propertyName or object.propertyName, but when I output those values to the console without using this or object name, 1st console.log returns an empty string and the other one returns the uncaught reference error. are they pointing to the window object? then in both the cases, it should return a uncaught reference, can somebody here please clarify this.. thanks in advance. :)

var car = {
    name : "ford",
    color:"red",
    log : function(){

         console.log(name);
        // outputs an empty string

         console.log(color); 
        // Returns error (this.js:8 Uncaught ReferenceError: color is not defined)

    }
}

car.log();
2
  • 1
    The variables name and color do not exist in your scope, so JavaScript is looking in the outer and then global scopes for them. Your name is actually referring to window.name. Commented Jan 10, 2017 at 16:53
  • @RocketHazmat Thank you Commented Jan 11, 2017 at 5:57

3 Answers 3

4

Try console.log(this.name) and console.log(this.color).

Additional Information from MDN

When a function is called as a method of an object, its this is set to the object the method is called on.

In the following example, when o.f() is invoked, inside the function this is bound to the o object.

Source: MDN

Sign up to request clarification or add additional context in comments.

1 Comment

I think you mean this.color.
2

The reason you don't get a ReferenceError when outputting name is that browsers have a built-in global called name: It's the name of the current window. But they don't have a built-in global called color, so you get the error.

To access name and color on your object when you use car.log(), you'd use this.name and this.color.

5 Comments

This global name is not same as window.name, is it? Because if we replace name with window.name in this code we get undefined.
@MohitBhardwaj: Yes, it is, and no, window.name won't be undefined (provided that's the default window global, not something else).
yeah sorry, you're right. It's window.name and it consoles blank string not undefined. Thanks :)
@T.J.Crowder I really Thank you for your answer. Is there any special purpose for setting window.name to an empty string?
@seshukumar: It's just the default. The name wouldn't be "" if it were opened with a link naming it, e.g.: <a href="..." target="wnd42">...</a> (the window's name would be "wnd42"), or via window.open (window.open("...", "wnd42")).
0

yes you are correct both should have thrown uncaught reference but wait ....

actually there is a property on window which is .. yeah ... name

so actually you console that property of window.. ie window.name

.. second one is just correct .. uncaught reference

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.