in Javascript, I am creating an object like this:
var testObject = {
value: "this is my initial value",
setup: function() {
value: "foo"
}
};
Now, I would like to be able to instantiate this object, so I am trying this:
var myFirstObject = new testObject();
var mySecondObject = new testObject();
so that when I call .setup() it will change the value only for that referenced newly created object. How can I achieve this? This code does not seem to work.
You don’t instantiate objects, you instantiate functions.
EDIT:
As per your comment, here’s how you would bind to the DOM using functions on this object:
Bear in mind that you won’t have any guarantee that the click handler will be executed in the context of your object (i.e. in your click handler,
thismight not be yourtestObjectinstance). If yourclickHandlerintends to modify the object’s instance variable in any way, it’s better to ensure the context like so: