The "this" property.
Moderator: General Moderators
The "this" property.
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.
If your not sure what I'm talkin' about when I say "this" I mean: this.styleorwhatever; in Javascript...
Thanks for reading my post.
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..
I am about 15% through my js book but I believe that is correct.
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
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.
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.
- MrPotatoes
- Forum Regular
- Posts: 617
- Joined: Wed May 24, 2006 6:42 am
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 nothingHere is an example from my book. Hopefully it won't be removed for copyright infringment.
Might want to try:
Code: Select all
function rectangle(w, h) {
this.width = w;
this.height = h;
}
var rect1 = new rectangle(2, 4); // rect1 = { width:2, height:4 };
Code: Select all
alert(rect1.width);
alert(rect1.height);
Code: Select all
<input name="txt_example" type="text" value="Enter Text" onBlur="alert( this.value );">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 );">