Possible Duplicate:
Javascript: Do I need to put this.var for every variable in an object? 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".
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".
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".
- 76.8k
- 10
- 123
- 160
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".
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".
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".
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".
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".
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".