Yetimwork Beyene
Yetimwork Beyene

Reputation: 249

How to get the object name of "this" in either javascript or/and jquery?

I'm reading someone else plugin code to gain a better understanding because of poor documentation. I'm seeing a lot of "this" objects and need to know the name of its object that it refers to for the "this" object? I'm not sure if "this" object is in jquery or javascriopt..... I hope this is a value question because I have no starting point code in obtaining "this" object name.. Also not knowing if "this" object was created as a literal object or constructor object

Upvotes: 0

Views: 90

Answers (2)

Lam Phuong Lam
Lam Phuong Lam

Reputation: 16

You can use the following:

this.nodeName
this.tagName

or

$(this).prop("tagName")
$(this).prop("nodeName")

Upvotes: 0

Jeff
Jeff

Reputation: 12785

this in a method refers to whatever object the method was called from.
So, for example take the following code block:

var x = {};
x.myFunc = new function(){this.foo = 1;}
x.myFunc();
alert(x.foo);

After this, alert will run and display 1

This is the most common way of getting a this. You can also use the call method to manually set an object as this for the purposes of running that method.

If there's no scope for this to come from (i.e. it's not called on an object) this will refer to with window element.

Upvotes: 1

Related Questions