0

I have this CODE:

var MyObj = (function() {
    //Constructor
    var MyObj= function(){
        this.myArray = [1,2,3];
    }

    MyObj.prototype = {
        myFunc: function(){
            alert(this.myArray.toString());                
        },
        myFuncCaller: function(){            
            MyObj.prototype.myFunc();                
        }
    };
    return MyObj;      
})();

var myObj = new MyObj();
myObj.myFunc();

//This line will throw an exception because this.myArray is undefined
myObj.myFuncCaller();

​ Why is this.myArray undefined? I know I'm doing something wrong, how would be the correct way to do it?

1 Answer 1

4

Just use this:

this.myFunc();

When you call a function in Javascript, this is set to the expression you called it on.
In your code, for example, this in myFunc() is MyObj.prototype.

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

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.