0

Hello I am a javascript beginner and I read about how to interpret the this keyword but in this example I am still confused:

<script> function example(param) { this.a = param; var b = true; this.getB = function() { return b; } this.setB = function(x) { b = x; } } document.write(window.a); //prints "undefined" (line A) document.write(window.b); //prints "undefined" (line B) document.write(window.getB(); //generates an error "undefined is not a function (Line C)" 

For Line A, I guess the explanation is that this refers to the object that owns the function example, in this case, the window object. Therefore you can reference window.a, but it is not defined so it prints undefined

I cannot understand Line B. Isn't var b restricting b to a local scope? Which should mean that you can only reference b within the function? I was expecting Line B to generate an error, not printing undefined.

I have totally no clue about Line C, why does it generate an error, and what does this mean in this case?

Many thanks!

9
  • 1
    developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… Commented Feb 25, 2015 at 21:27
  • this depends on how you call the function. new example().getB(). Commented Feb 25, 2015 at 21:27
  • Keyword "this" is referred toa global scope, as you can see in developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… Commented Feb 25, 2015 at 21:28
  • this. whatever != whatever, unless something is in global scope. Commented Feb 25, 2015 at 21:29
  • 1
    You have never actually called example(). None of the code inside it gets executed. Commented Feb 25, 2015 at 21:29

3 Answers 3

0

The this variable depends on how the function is being used.

If you instantiate the function (treat it like a class) then this will refer to the instance of the class:

new example("foo").getB(); // `this` is an instance of example 

If you call it with Function.prototype.call then this will refer to whatever object you passed to the call method:

example.call(anotherObject, "foo"); // `this` refers to anotherObject 

If you just execute it directly then this may refer to the window or whatever other surrounding scope.

example("foo"); // `this` likely refers to the window object 

You can also use Function.prototype.bind to wrap your function in a specific scope to help clear things up:

var wrappedExample = example.bind(aSpecificObject); wrappedExample(); // Doesn't matter how it's called, `this` will refer to aSpecificObject 

Of course you may want to consider your target browsers before using bind (or use a polyfill).

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

2 Comments

No problem! This is a common area of confusion for people who are new to JavaScript. :)
@user1783398 you should accept one of the answers here if it has answered your question.
0

First - you never call the example() function. So nothing it does has any effect.

Second - you have a syntax error on the last line, you are missing a ).

If you were to fix those issues, the results would be different:

function example(param) { this.a = param; var b = true; this.getB = function() { return b; } this.setB = function(x) { b = x; } } example("data"); document.write(window.a); //prints "undefined" (line A) document.write(window.b); //prints "undefined" (line B) document.write(window.getB()); //generates an error "undefined is not a function (Line C)"

For Line A, I guess the explanation is that "this" refers to the object that owns the function "example", in this case, the window object. Therefore you can reference window.a, but it is not defined so it prints "undefined"

It wasn't defined because you weren't calling the function. If you were to call it, then it is defined as the value of param.

I cannot understand Line B. Isn't "var b" restricting b to a local scope? Which should mean that you can only reference b within the function? I was expecting Line B to generate an error, not printing "undefined".

The locally scoped variable b is distinct from the b property of the object that is stored in this.

Accessing an undefined property of an object doesn't throw an error in JavaScript, it gives you undefined.

If you were to access an undeclared variable, then you would have a Reference Error.

I have totally no clue about Line C, why does it generate an error, and what does "this" mean in this case?

As with the other examples, you don't use this for lines A, B or C. You use window. this is only used inside the function.

You were getting an error because you never ran the function, so window.getB was never set. Calling the undefined value as a function throws an error.

If you fix the problem, as described above, you get the value of the local b variable.

Comments

0

If you are using this within a function, it will refer to the global window object.

function whatsThis(){ console.log(this); } whatsThis(); //displays the window object 

If the function exists within an object created by a constructor function, however, this will refer to the parent object

var WhatsThis = function(){ this.showThis = function(){ console.log(this); }; }; var obj = new WhatsThis(); obj.showThis(); //displays the instance of WhatsThis 

Likewise with object literals, this refers to the containing object.

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.