1

I am left scratching my head here. I am pretty new with both JSON and Javascript so I am wondering how I would go about this.

Say I have an object:

MyObject.prototype = {
    // different methods and properties
    _randomMethod: function MyObject_randomMethod() {
         MyObject.myArray = [];
    },
};

How do I declare an array property for my object (like above: MyObject.myArray = [];) and have it available throughout the object so I can access it in other methods.

Maybe this has already been covered and I am just not using the right terminology but if someone could help me out, I'd appreciate it since I can't figure it out myself.

Just so its clear, I want to declare this array property dynamically within a method like the example above and then be able to use it in other methods within this same object with the 'this' reference or something similar.

1 Answer 1

3

Use this to refer to the current instance:

MyObject.prototype = {
    // different methods and properties
    _randomMethod: function MyObject_randomMethod() {
         this.myArray = [];
    },
};

http://jsfiddle.net/5jSe3/

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.