0

I have two objects of the same type.

function myObject(){
this.a = 1;
this.b = 1;
function changeA(){//some code};
function changeB(){//some code};
}

var obj1 = new myObject();
var obj2 = new myObject();

How can I make a call to obj2.changeB() from external code, another function or another object (e.g. obj1) ?

2
  • You can't, the functions are local and not part of the myObject Commented Sep 7, 2014 at 14:51
  • A local function is not a method. Commented Sep 7, 2014 at 14:58

3 Answers 3

3

obj2.changeB() doesn't exist.

You need to assign a property on your object, not create a local variable:

this.changeB = function() { ... };
Sign up to request clarification or add additional context in comments.

Comments

0

Just create a properties in you object like:

function myObject(){
this.a = 1;
this.b = 1;
this.functionA = function changeA(){//some code
    alert('im 1');
};
this.functionb = function changeB(){//some code
alert('im 2');};
}

and call the function obj2.functionb();

LIVE DEMO

Comments

0

You have to do something like that:

var myObject = function(){
var protectedValue1 = ‘variable’;
var protectedValue2 = ‘variable’;
var changeA = function(){
alert(protectedValue);
}
var changeB = function(){
alert(protectedValue);
}
}
var object1 = new myObject();
var object2 = new myObject();
//
object2.changeB();

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.