The "this" property.

JavaScript and client side scripting.

Moderator: General Moderators

User avatar
JellyFish
DevNet Resident
Posts: 1361
Joined: Tue Feb 14, 2006 7:18 pm
Location: San Diego, CA

The "this" property.

Post by JellyFish »

I'm not sure if "this" is a property or object in javascript. I'd like to know what it does, cause I haven't imprinted the concept in my brain yet...

If your not sure what I'm talkin' about when I say "this" I mean: this.styleorwhatever; in Javascript...

Thanks for reading my post.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

In javascript, I believe it's object/property/method . whatever

So if you have an array with 10 values and the name of the array is devnet, you can access the count with something like..

Code: Select all

count = devnet.length;
alert(count); // should say 10
I am about 15% through my js book but I believe that is correct.
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

"this" is a way to access object properties and methods from within said object. "this" is an object-oriented concept. Look up Object-Oriented or OOP on this site and on Google
User avatar
JellyFish
DevNet Resident
Posts: 1361
Joined: Tue Feb 14, 2006 7:18 pm
Location: San Diego, CA

Post by JellyFish »

I meant the "this" keyword. :D
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

so did I

EDIT: read about "this" on php.net
http://us2.php.net/manual/en/language.oop.php
It's the same concept in javascript, so read up.
User avatar
MrPotatoes
Forum Regular
Posts: 617
Joined: Wed May 24, 2006 6:42 am

Post by MrPotatoes »

i use the this object in C++ because ti makes it alot easier to see what is coming from where.

in C/C++ you honestly don't have to call "this" from within your classes. but explicitly saying it is easier for me to read personally
User avatar
JellyFish
DevNet Resident
Posts: 1361
Joined: Tue Feb 14, 2006 7:18 pm
Location: San Diego, CA

Post by JellyFish »

Soz Goat-Guy, late post(I was refering to astions post).

So basically, "this" keyword gets the referance to the next element or object above in the tree of objects. If that makes any sence?
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

here's an example in php, because I don't know javascript that well (but it's the same concept)

Code: Select all

class someClass{
    var $someVar = "foobar";
    function someMethod(){
        // I would like to print this object's $someVar property from within this object, so I must use $this to access it
        echo $this->someVar; // prints "foobar"
        echo $object1->someVar; // throws error and prints nothing
    }
}
$object1 = new someClass;
// now I would like to access $someVar outside of the object, so I would have to use $object1 instead of $this
echo $object1->someVar; // prints "foobar"
echo $this->someVar; // throws error and prints nothing
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

Here is an example from my book. Hopefully it won't be removed for copyright infringment.

Code: Select all

function rectangle(w, h) {
  this.width = w;
  this.height = h;
}

var rect1 = new rectangle(2, 4); // rect1 = { width:2, height:4 };
Might want to try:

Code: Select all

alert(rect1.width);
alert(rect1.height);
User avatar
hawleyjr
BeerMod
Posts: 2170
Joined: Tue Jan 13, 2004 4:58 pm
Location: Jax FL & Spokane WA USA

Post by hawleyjr »

Code: Select all

<input name="txt_example" type="text" value="Enter Text" onBlur="alert( this.value );">
Add this to a HTML page. You'll notice that in this case 'this' is the text box. this has other properties also. and if you wanted you could do something like this:

Code: Select all

<script language="javascript">
function jsfun( txtObj ){

alert( txtObj.name );
alert( txtObj.id );
alert( txtObj.value );
}
</script>
<input name="txt_example" id="txt_id" type="text" value="Enter Text" onBlur="jsfun( this );">
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

hawleyjr wrote:You'll notice that in this case 'this' is the text box.
And the reason for that is that javascript considers that input as an object (see DOM)
User avatar
hawleyjr
BeerMod
Posts: 2170
Joined: Tue Jan 13, 2004 4:58 pm
Location: Jax FL & Spokane WA USA

Post by hawleyjr »

The Ninja Space Goat wrote:
hawleyjr wrote:You'll notice that in this case 'this' is the text box.
And the reason for that is that javascript considers that input as an object (see DOM)
Correct :)
User avatar
JellyFish
DevNet Resident
Posts: 1361
Joined: Tue Feb 14, 2006 7:18 pm
Location: San Diego, CA

Post by JellyFish »

Wow. A lot of examples, exceeds my expectations. Thanks for all the post guys, I really appreciate these. :D
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

you gotta remember jellyfish... we're freakin' awesome. Don't you EVER forget it. We're FREAKING AWESOME!!
User avatar
MrPotatoes
Forum Regular
Posts: 617
Joined: Wed May 24, 2006 6:42 am

Post by MrPotatoes »

i concur
Post Reply