This is just a simple question. Either way works. I prefer my first example, but I didn't know if doing it this way causes more memory to be allocated than the second example since we are calling "new" on the object....
Example 1
var post = function(){
var self = this;
self.div = $('<div></div>');
self.color = function(color){
this.div.css({background:color});
}
}
var p = new post();
p.color("#FFF");
Example 2
var post = function(){
self = this;
self.div = $('<div></div>');
}
var color = function(p, color){
p.div.css({background:color});
}
var p = new post();
color(p, "#FFF");
So, in the first example, the color function I believe will get recreated everytime a new post is called. What if I have a 100 new post();
calls. Is is less efficient than Example 2 where the function is only defined one time?
Does that make sense what I'm asking?