Page 1 of 2
The "this" property.
Posted: Thu Sep 28, 2006 11:53 am
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.
Posted: Thu Sep 28, 2006 12:13 pm
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.
Posted: Thu Sep 28, 2006 12:21 pm
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
Posted: Thu Sep 28, 2006 12:22 pm
by JellyFish
I meant the "this" keyword.

Posted: Thu Sep 28, 2006 12:27 pm
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.
Posted: Thu Sep 28, 2006 12:30 pm
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
Posted: Thu Sep 28, 2006 12:45 pm
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?
Posted: Thu Sep 28, 2006 1:00 pm
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
Posted: Thu Sep 28, 2006 1:06 pm
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);
Posted: Thu Sep 28, 2006 1:42 pm
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 );">
Posted: Thu Sep 28, 2006 1:49 pm
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)
Posted: Thu Sep 28, 2006 1:52 pm
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

Posted: Thu Sep 28, 2006 2:04 pm
by JellyFish
Wow. A lot of examples, exceeds my expectations. Thanks for all the post guys, I really appreciate these.

Posted: Thu Sep 28, 2006 2:10 pm
by Luke
you gotta remember jellyfish... we're freakin' awesome. Don't you EVER forget it. We're FREAKING AWESOME!!
Posted: Thu Sep 28, 2006 2:14 pm
by MrPotatoes
i concur