Typically, when I create a library, my approach is as follows:
var myLibrary = (function() {
return {
publicProperty: 'test',
publicMethod: function() {
console.log('public function');
},
anotherMethod: function() { //... },
// .. many more public methods
};
}());
I recently heard that there may be a difference in speed or memory usage during initialization if the library is written like this:
var MyLibrary = function() {
this.publicProperty = 'test';
};
MyLibrary.prototype = {
publicMethod: function() {
console.log('public method');
},
anotherMethod: function() { //... },
// ... many more public methods
};
myLibrary = new MyLibrary();
Is one of these methods faster to initialize than the other? Does my question even make sense? I assume both achieve the same goal (whereby I can use myLibrary.publicMethod()
elsewhere in my code after document readiness). Thank you!