Skip to content

Instantly share code, notes, and snippets.

@TiagoWinehouse
Created September 12, 2015 13:05
Show Gist options
  • Save TiagoWinehouse/4a48b82e654fe5c78c0a to your computer and use it in GitHub Desktop.
Save TiagoWinehouse/4a48b82e654fe5c78c0a to your computer and use it in GitHub Desktop.
Performance Closures
//exemplo pouco prático porém demonstrativo
function MyObject(name, message){
this.name = name.toString();
this.message = message.toString();
this.getName = function(){
return this.name;
};
this.getMessage = function(){
return this.message;
};
}
// código anterior não aproveita os benefícios dos closures e portanto poderia ser reformulado assim:
function MyObject(name, message){
this.name = name.toString();
this.message = message.toString();
}
MyObject.prototype = {
getName : function(){
return this.name;
},
getMessage: function(){
return this.message;
};
}
//Ou assim:
function MyObject(name, message){
this.name = name.toString();
this.message = message.toString();
}
MyObject.prototype.getName = function(){
return this.name;
};
MyObject.prototype.getMessage = funtion(){
return this.message;
};
//No dois exemplos anteriores, o protótipo herdado pode ser compartilhado por todos os objetos e
//as definições de métodos não precisam ocorrer sempre que o objeto for criado.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment