Created
April 9, 2013 08:23
-
-
Save ceth-x86/5343952 to your computer and use it in GitHub Desktop.
Javascript : Dynamically Selecting a Method / Property
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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