samedi 31 janvier 2015

Javascript - What problems are associated with declaring a prototypical method of a parent function *inside* the parent function's body?


Stylistically, I prefer this structure:



var Filter = function( category, value ){
this.category = category;
this.value = value;

// product is a JSON object
Filter.prototype.checkProduct = function( product ){
// run some checks
return is_match;
}

};


To this structure:



var Filter = function( category, value ){
this.category = category;
this.value = value;
};// var Filter = function(){...}

Filter.prototype.checkProduct = function( product ){
// run some checks
return is_match;
}


Functionally (har har), are there drawbacks to stucturing my code this way? Will adding a prototypical method to a parent function object inside the parent function's body ( i.e. before I even finish my parent function's expression statement ) cause unexpected scoping issues?


I've used the first structure before with success, but I want to make sure I'm not setting myself for a debugging headache, or causing a fellow developer grief and aggravation due to bad coding practices.





Aucun commentaire:

Enregistrer un commentaire