i would like to create a new object in javascript (using simple inheritance) such that the class of the object is defined from a variable:
var class = 'Person';
var inst = new class
any ideas?
i would like to create a new object in javascript (using simple inheritance) such that the class of the object is defined from a variable:
any ideas? |
|||
|
You can do something like
The key is just referencing the object that owns the function that's the constructor. This works fine in global scope but if you're dumping the code inside of a function, you might have to change the reference because EDIT: To pass parameters:
|
|||||||||
|
This talk was really helpful when I got into different inheritance patterns in javascript. Example code is included in the second link (the video introduces it). http://alexsexton.com/inheritance/demo/ HTH |
|||
|
Try
|
|||
|
Here's how I do it. Similar to meder's answer.
|
|||
|