0

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.

2
  • Since MyFunction isn't a constructor, better name it myFunction Commented May 4, 2014 at 13:09
  • More on constructor functions, prototype and the value of this can be found here: stackoverflow.com/questions/16063394/… Commented May 4, 2014 at 13:35

4 Answers 4

2

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();

FIDDLE

Sign up to request clarification or add additional context in comments.

Comments

1

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();

Comments

1

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();

Comments

1

(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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.