ajsie
ajsie

Reputation: 79686

Check what context "this" refers to in javascript

I want to know what context "this" refers to at a given time in my code.

I know there are rules but there has to be a way to check it by code.

How do I do that?

Upvotes: 0

Views: 515

Answers (3)

sova
sova

Reputation: 5650

Like what superfro is trying to say, although you said "context" when you're talking about

this

it's actually an object, you are able to ask for its properties and methods via the dot operator.

this.variable;

But if you want to know context as in where you are in a nested conditional or something, you basically have to just rely on which object you are manipulating. Could you post an example of what you want/need?

Upvotes: 0

ChaosPandion
ChaosPandion

Reputation: 78262

As long as you are expecting a set of known types you can do this.

if (obj instanceof Object1) {

} else if (obj instanceof Object2) {

} else if (obj instanceof Object3) {

}

Upvotes: 4

superfro
superfro

Reputation: 3302

The type IS object, If you want more details of what the object contains you can install firebug for firefox and use console.log(this), you should be able to click on it then and see th econtents. but it IS an object.

Upvotes: 2

Related Questions