Created
March 6, 2013 10:32
-
-
Save jineeshjohn/5098412 to your computer and use it in GitHub Desktop.
String into a javascript function call. Better way to create instance for dynamic class names
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
var Y = (function(){ | |
return { | |
Car:function(model){ | |
this.model = model; | |
this.applyBreak = function(){ | |
alert("done break!!"); | |
} | |
} | |
} | |
})(); | |
var json = { | |
className:"Car" | |
} | |
//Solution 1 with eval - bad | |
var str = "new "+json['className']+"(2007)"; | |
var cc = eval(str); | |
//Solution 2 subscript notation -- better | |
function createInstance(name,param){ | |
return new Y[name](param); | |
} | |
var cc = createInstance(json['className'],2007); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment