With the prototype, your objects shared the same "table" of functions, (inheriting the prototype functions), while declaring the functions within the constructor will create pointer for each table of each instance, allows you to decide with loops whether or not to declare a function, or if it will be different from the other instance, they may also be individually superscripted after...
the prototype helps to standardize and decreases the use of memory...although it is very little...
[Obj][Obj][Obj][Obj]
v v v v
[prototype (ponteiro para função_1) (ponteiro para função_2)]
[Obj (ponteiro para função_1) (ponteiro para função_2)]
[Obj (ponteiro para função_1) (ponteiro para função_2)]
[Obj (ponteiro para função_1) (ponteiro para função_2)]
[Obj (ponteiro para função_1) (ponteiro para função_2)]
then, the prototype would be the 'Static' class, properties common to all instances, as in a language C like..., but as javascript is much more permissive than a compiled language, it is a little difficult to compare, I hope I helped..., although it seems a little confusing...
+OBS: properties can also be assigned to the prototype, since functions are actually treated as variable in javascript.
what makes the following scenario possible:
var minha_string="string qualquer";
String.prototype.CONSTANTE_TEXTO = "TEXTO";
console.log(minha_string.CONSTANTE_TEXTO);
console.log("outra string".CONSTANTE_TEXTO);
//sim, os dois retornaram "TEXTO" ao console
and the addition of hooks (Hooks) in already declared objects code, without having to access each one, which can be useful to find out how some code works...
https://answall.com/questions/44191/qual-a-diferença-entre-utilizar-o-metodo-da-classe-o-prototype-javascript Hello, from what I could understand by the answer I found in the link above the big difference is in memory and uniqueness. also has this very cool Devmedia text on prototype that might help you ! https://www.devmedia.com.br/prototype-javascript/7063 I hope I helped you !!
– Pedro Valcarenghi
This answers your question? What’s the difference between using the class method and the Javascript prototype?
– hkotsubo
It is not the main difference, but in the first case btnLeft will only exist for instances, that is to say, manipulating the "class" to overwrite it will be impossible in the way you did, whereas in the second the prototype is the object of the "class" (I am calling it a class but it is not quite the correct name)then the prototype returns an object that if modified will represent for all existing instances. There’s a little more to it, but I’ll see if you don’t have a more complete answer on the subject.
– Guilherme Nascimento
@Guilherme Nascimento, because on the property
prototype
shows the defined methods, but not the properties was not to demonstrate everything? since in the propertyprototype
is defined the things that will be inherited?– Mirian cherbo
Not only that, it’s much more, as I said in the prototype the behavior is different, in the first code only exists in instances, in the second there is an object that represents all instances of a class and even in the class (not in the instance) there will be an "object" inside that represents the interface of what you describe.
– Guilherme Nascimento