I am trying to understand difference between Prototype functions and nested functions. I Need to know the following
- Which is better , performance wise
- What is the main difference between the two
- Which structure is better suited for which situations (I assume both have different aim) ?
My Basic usage : My basic usage is I want to write a a main function for web app that when initiated, it will created menus, buttons , button clicks events, draw charts,make tables etc. during app navigation and i need my code to be structured in a better and fastest way. And I will be using jquery and its plugins alot *.
For simplicity of usage , consider I need to create portlets/widget container at many places/stages in may app, and I would just call var port = App.creatPortlet() and then port.content(// place data)
Kindly Help.
Performance: I created performance test here prototype-vs-nested-function and it seems PROTOTYPE function is faster. But I need some advice on it.
PROTOTYPE FUNCTION:
function Person(opt) {
this.firstName = opt.firstName;
this.lastName = opt.lastName;
}
Person.prototype.getFullName = function() {
return this.firstName + " " + this.lastName;
};
// Testing performance
var P1 = new Person({
firstName: 'Jeremy',
lastName: 'McPeak'
}).getFullName();
NESTED FUNCTION:
var Person = function(opt) {
return {
getFullName: function() {
return opt.firstName + " " + opt.lastName;
}
};
}
// Testing performance
var P1 = new Person({
firstName: 'Jeremy',
lastName: 'McPeak'
}).getFullName();
UPDATE: http://jsperf.com/closure-prototype-static-reveal I created benchamrk according to my exact need.