Created
September 12, 2015 13:05
-
-
Save TiagoWinehouse/4a48b82e654fe5c78c0a to your computer and use it in GitHub Desktop.
Performance Closures
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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