Possible Duplicate:
Javascript: Do I need to put this.var for every variable in an object?
I'm struggling to understand functions and objects in javascript. It is said that also functions are objects and objects are kind of "associative arrays" i.e. collections of key-value pairs. I understand that if I write
function myFunction() {
var value = 0;
}
alert(myFunction.value); //then this gives me "undefined"
because variables have function scope. But if I write
function myFunction() {
this.value = 0;
}
alert(myFunction.value); //then this gives me "undefined" too.
But finally, If I write
function myFunction() {
this.value = 0;
}
myFunction.value = 0;
alert(myFunction.value); //then this gives me 0
So I can give myFunction property "value" but from "outside". Can someone explain what is going on and why this.value = 0; doesnt create property "value".
You need to create a
instance
by using the new keyword.Now this corresponds to the current object , and it gets you the corresponding value of the property.
Check Fiddle
At end of the day .. functions are still objects.. So it does not complain when you assign
myFunction.value = 0
.. It might be confusing as you are using the value (key) both inside and outside the function.. Replace it withBut it won't be reflected inside the actual myFunction as you have not called the function yet.
In javascript, any function is also an object, they are objects of
Function
, just asNumber, Object, Array
One tricky thing is the
new
word, then it prefix before a function, it create a new object, and makethis
keyword pointer to that new object (one more, it assign that function prototype to the new object __ proto __).In function,
would create a new property
value
to the new object, and assign 0 to it.If there is no
new
before function, it's function call, andthis
will pointer to Window object.Try to
console.dir(this);
in the function, you will see the difference.would create property
value
tomyFunction
, and assign 0 to it. Because myFunction is just an object (of Function).myFunction
is a function object. You can pass it around, assigned it to variables, assign properties to it and you can call it.Assigning properties works like with any other object:
But note that at this point, you have not called the function yet, so the code inside the function (
var value = 0;
orthis.value = 0;
) was not even executed yet. Consider this:When you execute the function with
myFunction()
, only then the local variable is created / a property is set onthis
. Whatthis
refers to depends on how the function is called and is well explained in the MDN documentation.this
never refers to the function itself unless you explicitly set it so.Not sure I can clear up all of the nuances for you, but this may shed some light:
Using the
new
keyword creates a new 'instance' ofmyFunction
allowingthis
to be used to assign a value from inside the function.Let's look at all three cases individually:
Here, you're declaring a variable in the function's scope. Each time the function is called, the variable will be created (and memory will be allocated). When the function returns, the variable goes out of scope - the variable
value
is flagged and will be GC'ed. The scope can't be accessed from a scope "higher" than this function's scope... if this function defined a function within its scope, that function will have access to the variablevalue
(look into closures for more details). Bottom line: the variable only exists as when the function is called, and won't exist after the function returns.Here, you're defining a function that could be a constructor, a method, an event handler or a combination of all of the above.
this
is a reference that will point to the context in which the function is called. This contexted is determined "ad hoc" and may vary:In the last case:
It wouldn't have made any difference if you'd have written this:
Because, as I explained above:
this
references whatever the context is at the time the function is called. This needn't bemyFunction
, in fact: more often than not it won't be:If you want to access a function's properties inside that function, the easiest way is to reference that function like any other object:
Caution:
Just a friendly warning: it's not very safe to use
this
in functions without checking for globals... If a function is called without an explicit context, JS uses the global (window
) object by default. This means that every line that assigns a property to whatever objectthis
happens to be pointing too will set a global variable:A few ways to prevent the global object from being cluttered with globals:
You misunderstood the javascript prototype/object concepts.
For the first example you are right, the variable has a function scope
The second example is wrong. If you want to use a function as 'class' you have to create an object from it
only then you can access the 'value' property of it. for every 'new' statement a new object is created.
In third example you add a static property to function which can be accessed without creating an object. Different technique that's
Hope it helped