Skip to content

Instantly share code, notes, and snippets.

@ceth-x86
Created April 9, 2013 08:23
Show Gist options
  • Save ceth-x86/5343952 to your computer and use it in GitHub Desktop.
Save ceth-x86/5343952 to your computer and use it in GitHub Desktop.
Javascript : Dynamically Selecting a Method / Property
function MyClass(name) {
this.name = name;
}
MyClass.prototype.say_hello = function() {
alert(this.name);
}
MyClass.prototype.say_something = function(phrase) {
this.name = phrase;
this.say_hello();
}
var s = new MyClass("hello");
alert(s['name']); // property
s['say_hello'](); // method
s['say_something']('bye'); // method with parameter
var flag = true;
s[flag ? 'say_hello' : 'say_hello']();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment