javascript - js jquery Prototype functions vs nested functions -


i trying understand difference between prototype functions , nested functions. need know following

  1. which better , performance wise
  2. what main difference between two
  3. which structure better suited situations (i assume both have different aim) ?

my basic usage : basic usage want write a main function web app when initiated, created menus, buttons , button clicks events, draw charts,make tables etc. during app navigation , need code structured in better , fastest way. , using jquery , plugins alot *.

for simplicity of usage , consider need create portlets/widget container @ many places/stages in may app, , call var port = app.creatportlet() , port.content(// place data)

kindly help.

performance: created performance test here prototype-vs-nested-function , seems prototype function faster. need 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 created benchamrk according exact need.

well first of performance tests such small portions of code not useful because don't know if reflect performance in real use case scenario. (it happen test optimizer of js engine small code, e.g. inlining of methods or trigger optimizing process of js engine take longer initialize bring performance boosts in realtime applications, detected flaw of testing library, ....)

i bet don't test prototype vs nested functions here two assignments vs scope handling.

looking @ test, don't test both cases. test case 2 (in preparation overwrite access name function person var person = ...;, function person never used in test). should this: updated jsperf.com.
(edit looks changed while writing).

anyway difference , better. both have valid use cases , determine better.

object type

for first one, every object created person of same type:

 var p1 = new person();  var p2 = new person();   console.log( p1 instanceof person);  //true  console.log( p2 instanceof person);  //true 

while second example every object create of type object.
first 1 you, if object passed function, can test if of type person, while second 1 can't.

while prefer test existence of feature/function instead of testing type, - me - make no difference.

prototype chain

sometimes useful updated/extend functionality objects or modify/extend existing function objects of 1 type.
while possible first example not possible second one.

var person = function() { };  var p1 = new person(); person.prototype.dosomething = function() {console.log("....");};  p1.dosomething(); 

reuse of code

with first example can reuse functions on objects similar given one. this:

person.prototype.dosomething.call(similarobject, ...); 

which handy in situations. example use array functions on array like objects.

protection of raw data

sometimes useful protect raw data being read/modified directly. is strength of second example. while thing should handled documentation instead of forcing code still valid use case. in example firstname , lastname hidden outside, except code part creates the person.

performance , speed

which 1 performs better in speed , memory usage depends on js engine. not every engine create new function nested functions, more correctly detected , references on second call. scope creation have overhead, negligible depending on object count , how frequent create them.

but said have other things in code create bottlenecks.

in of cases using prototype better choice of respect maintainability , readability.


Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -