1

Okay see the following code:

function person(name){
    this.name = name;
    this.say = function(){
        alert(this.name);
    }
};

Main = {};

Main.person1 =  new person("p1");

Main.person2 =  new person("p2");

Main.person3 =  new person("p3");

executeSay = function(argument1){
 //Implementation
}

What executeSay should do is, call the say method of the given argument, I am not sure how it goes but let me put this way executeSay("person1") should execute Main.person1.say() and so on. I think we can accomplish this by call method but I am not sure about the implementation.

Please don't suggest the following approach

say = function(){
  alert(this.name);
}
say.call(Main.person1);
pb2q
  • 58,613
  • 19
  • 146
  • 147
Mudassir Ali
  • 7,913
  • 4
  • 32
  • 60
  • Re `executeSay(person1)`: What is `person1` here? Is it a variable? Is it supposed to be string? Is it supposed to be `Main.person1`? You basically already have your answers (`Main.person1.say()`) I'm confused about what you are having troubles with. – Felix Kling Oct 12 '12 at 10:06
  • I've updated the question, the argument1 must be of string type... – Mudassir Ali Oct 12 '12 at 10:11
  • possible duplicate of [Dynamic object property name](http://stackoverflow.com/questions/4244896/dynamic-object-property-name) – Felix Kling Oct 12 '12 at 10:15

4 Answers4

2

If you already pass the object in the function, you can access all its methods there, so use:

executeSay = function(person){
  person.say();
}

and then call this function by, e.g.,

executeSay( Main.person1 );
Sirko
  • 72,589
  • 19
  • 149
  • 183
  • I'm sorry for the confusion, I have updated the question, it shud be like executeSay("person1") should execute say method of the property "person1" in Main..which can be accomplished Main["person1"].say() ...I got the answer while typing this comment :P ! Thanks – Mudassir Ali Oct 12 '12 at 10:15
  • @Unspecified: You should not accept this answer if it is not the solution to your problem (that's just confusing for everyone else). Then either don't update the question or provide your own answer and accept it or vote to close it because it's a duplicate. – Felix Kling Oct 12 '12 at 10:17
2

I'd do it like this:

function Person(name){
  this.name = name
}

Person.prototype.say = function () {alert(this.name)}

var main = {
  person1: new Person('p1')
, person2: new Person('p2')
, person3: new Person('p3')
}

function executeSay(personStr) {main[personStr].say()}

(Updated to reflect the the string parameter for executeSay)

strandel
  • 21
  • 2
  • Note that the class name begins with a capital, whereas the local variable with a lower case letter. Also you need the "var" to not make the main variable a global one. – strandel Oct 12 '12 at 10:14
0

Let executeSay calls the say method on the argument object:

executeSay = function(person){
 person.say();
}

Demo.

Alberto De Caro
  • 5,147
  • 9
  • 47
  • 73
0

Is this not working?

executeSay = function(person){person.say()}
spicavigo
  • 4,116
  • 22
  • 28