I have a View
class that has a constructor method initialize
I think this is a constructor method, correct me if wrong.
var View = function(obj) {
this.initalize = obj.initalize;
};
What I would like to achieve is something gets called when the Class is instantiated.
How can I pass in an object like so?
var chatView = new View({
initialize: function() {
alert('Yay for initialization');
}
});
So that when I instantiate the View
I can pass an object to the constructor and within a key initialize
which value is a function and this key specifically gets called when instantiated.
I have a View
class that has a constructor method initialize
I think this is a constructor method, correct me if wrong.
var View = function(obj) {
this.initalize = obj.initalize;
};
What I would like to achieve is something gets called when the Class is instantiated.
How can I pass in an object like so?
var chatView = new View({
initialize: function() {
alert('Yay for initialization');
}
});
So that when I instantiate the View
I can pass an object to the constructor and within a key initialize
which value is a function and this key specifically gets called when instantiated.
- 1 Possible duplicate of Why is it impossible to change constructor function from prototype? – AMACB Commented Jan 25, 2016 at 2:53
- What I would like to achieve is something gets called when the Class is instantiated. Then call it. – user663031 Commented Jan 25, 2016 at 3:22
-
Ideally I can just pass an object, similar to Backbone.js github./jashkenas/backbone/blob/master/backbone.js I know they use
_.extend()
but the idea is when you instantiate aView
it does it on its own. – Michael Joseph Aubry Commented Jan 25, 2016 at 3:28
2 Answers
Reset to default 4If I get it right, there is a simple way of achieving what you want:
var View = function(obj) {
obj.initialize();
}
This way, the initialize function gets called whenever you instantiate a View class.
Be aware that if you want to do real "initialization code" inside the initialize function to work, you could use call (or apply):
var View = function(obj) {
if (typeof obj.initialize === 'function') {
obj.initialize.call(this);
}
}
var chatView = new View({
initialize: function() {
this.property = 'property';
}
});
console.log(chatView.property); // outputs property
Javascript doesn't have a constructor, remember that javascript is based on prototype. This is an example of "constructor" you can create
function Example (firstname, lastname) {
this.firstname = firstname;
this.lastname = lastname;
}
Example.prototype.getFullname = function () {
return this.firstname + ' ' + this.lastname;
}
If you want to create a constructor function you must to call it after you instantiate the function.
But this is a better structure you can use. I remended only if you need a constructor function and private function. Otherwise, use a simple structure with methods declared with prototype, you can get a better performance.
var MyObject = (function () {
// Constructor
function MyObject (foo) {
this._foo = foo;
}
function privateFun (prefix) {
return prefix + this._foo;
}
MyObject.prototype.publicFun = function () {
return privateFun.call(this, '>>');
}
return MyObject;
})();
var myObject = new MyObject('bar');
With this code you have a constructor, but it's a "private" function, so you can't overwrite it after instantiate the object.
Here I have a link I create testing differents structures: https://plnkr.co/edit/qzgWVZlnIFnWl0MoUe5n?p=preview
The result:
Test 1: ~15k (with private function) - Remended ONLY if you want/need a private function
Test 2: ~38k (with private function) - Not remended, it's returning an object which is really bad.
Test 3: ~8k (without private function) - Remended, it has the best performance, but you can't create a private function, which means, anybody can call any function :S