Reputation: 225
I am trying to access the variable myVar inside myObject from MyFunction, but get this error
Uncaught TypeError: Cannot read propperty 'myVar' of undefined
when running the code below.
var myObject;
function MyObject(){
this.myVar = 500;
MyFunction();
}
function MyFunction(){
alert(myObject.myVar);
}
myObject = new MyObject();
I have read this but still can't figure out how to access it.
Upvotes: 0
Views: 70
Reputation: 288120
The problem is that myObject
is assigned AFTER calling MyObject
, so if inside it, myObject
is still undefined.
Some alternatives:
function MyObject(){
this.myVar = 500;
myFunction.call(this); // Pass object as `this`
}
function myFunction(){
alert(this.myVar);
}
var myObject = new MyObject();
or
function MyObject(){
this.myVar = 500;
myFunction(this); // Pass object as argument
}
function myFunction(obj){
alert(obj.myVar);
}
var myObject = new MyObject();
or
var myObject;
function MyObject(){
this.myVar = 500;
}
function myFunction(obj){
alert(myObject.myVar);
}
var myObject = new MyObject(); // Now myObject is set
myFunction(); // Call `myFunction` after `MyObject`, not inside it.
or
var myObject;
function MyObject(){
this.myVar = 500;
this.myFunction(); // Prototype method
}
MyObject.prototype.myFunction = function(){
alert(this.myVar);
}
var myObject = new MyObject();
or
var myObject;
function MyObject(){
var that = this;
function myFunction(){ /* Place myFunction inside MyObject. Note that this
way each instance will have a copy of myFunction */
alert(that.myVar);
}
this.myVar = 500;
myFunction();
}
var myObject = new MyObject();
Upvotes: 1
Reputation: 7507
(I think you know myObject
and MyObject
are different things (even unrelated to the interpreter, just their names are similar), but maybe that caused your confusion, so I'll just state this.)
What the interpreter will do is first evaluate the left-hand side of the assignment (new MyObject()
) and then stuff it into the variable myObject
. This means, that when you call function MyObject() { ... }
the variable myObject
has not yet been initialised`. Obviously (now), it is undefined, hence the error.
There are different ways to solve this, Omri Aharon's is one possibility.
Upvotes: 1
Reputation: 7463
a good article you can find HERE
here is an example of defining a function like you wanted: FIDDLE
function MyObject() {
this.myVar = 500;
this.MyFunction();
}
MyObject.prototype.MyFunction = function() {
alert(this.myVar);
};
myObject = new MyObject();
Upvotes: 1
Reputation: 318202
When calling a function it's executed, and then the result is returned to the variable.
So, the function executes first, then the result is passed back to the variable
var myObject; // 1. new variable, undefined
function MyObject(){ // 3. execute function
this.myVar = 500; // 4. set a property
MyFunction(); // 5. call next function
}
function MyFunction(){
alert(myObject.myVar); // 6. myObject is still undefined,
} // result hasn't returned yet
myObject = new MyObject(); // 2. call function - 7. When everything is done
// executing, the result
// is returned
In other words, you can't do that.
One way to solve this would be to prototype the MyFunction and keep the value of this
constant
function MyObject() {
this.myVar = 500;
this.MyFunction();
}
MyObject.prototype.MyFunction = function() {
alert(this.myVar);
}
var myObject = new MyObject();
Upvotes: 2