6
I’m not getting literature in Portuguese, so I found an article in English that I didn’t quite understand the concept, someone could help me by explaining the differences:
Writing a Class with the methods within the Class:
function MyClass() { this.greet = function() { console.log('Hello!'); }; } var inst = new MyClass(); inst.greet(); // => 'Hello!'
Writing a Class with methods outside of Class:
//Classe aqui function MyClass() {} //metodo usando prototype ( escrito fora da Classe ) MyClass.prototype.greet = function() { console.log('Hello!'); }; var inst = new MyClass(); inst.greet(); // => 'Hello!'
Apparently it would look the same, but the author of these examples says that the first code is inefficient, so I would like someone to explain to me if this is true and why, either for truth or false statement of the author
I can say that if you want to create a private variable within Myclass, you won’t be able to access it if you
prototype
. In that case, the solution would be to declare it withthis
even.– Wallace Maxters
See http://answall.com/q/65131 and http://answall.com/q/44191
– bfavaretto