javascript tutorial - [Solved-5 Solutions] Use of ‘prototype’ vs.’this’ in javascript?
- javascript - java script - javascript array
Problem:
What's the difference between
and
Solution 1:
As others have said the first version, using "this" results in every instance of the class A having its own independent copy of function method "x". Whereas using "prototype" will mean that each instance of class A will use the same copy of method "x".
Here is some code to show this subtle difference:
As others have mentioned, there are various reasons to choose one method or the other. My sample is just meant to clearly demonstrate the difference.
Solution 2:
In most cases they are essentially the same, but the second version saves memory because there is only one instance of the function instead of a separate function for each object.
A reason to use the first form is to access "private members". For example:
Because of javascript's scoping rules, private_var is available to the function assigned to this.x, but not outside the object.
Solution 3:
The first example changes the interface for that object only. The second example changes the interface for all object of that class.
Solution 4:
The ultimate problem with using this instead of prototype is that when overriding a method, the constructor of the base class will still refer to the overridden method. Consider this:
versus:
If we think this is not a problem, then it depends on whether we can live without private variables, and whether we are experienced enough to know a leak when we see one. Also, having to put the constructor logic after the method definitions is inconvenient.
versus:
Solution 5:
What's the difference? => A lot.
WE think, the this version is used to enable encapsulation, i.e. data hiding. It helps to manipulate private variables.
Let us look at the following example:
Now, the prototype structure can be applied as following:
Different adults have different ages, but all of the adults get the same rights.
So, we add it using prototype, rather than this.
Related Searches to javascript tutorial - Use of ‘prototype’ vs ’this’ in javascript
javascript prototype vs this performancedifference between object method and object prototype methodjavascript prototype vs constructoradvantage of prototype in javascriptjavascript prototype this undefinedobject constructor vs prototypejavascript use prototype method in constructorjavascript prototype hoistingjavascript prototype vs constructorjavascript prototype vs this performancejavascript prototype vs object literaljavascript prototype methodswhen is angular a bad option?difference between constructor and prototype in javascriptobject constructor vs prototypejavascript why use prototypejavascript tutorialjava scriptjavascriptjavascript arrayjavascript booklearn javascriptjavascript codejavascript editorjavascript classjavascripjavascript debuggerjavascript onlinejavascript examplesjavascript testjavascript documentjavascript sliderwhat is javascriptjavascript form validationjavascript validatorhtml javascriptjavascript alertjavascript eventsjavascript printjavascript domjavascript objectjavascript functionhref javascriptjavascript datejavascript promptjavascript onclickjavascript returnjavascript forjavascript numberjavascript confirmjavascript onchangejavascript regular expressionjavascript ifjavascript variablejavascript timerjavascript cookiejavascript getelementbyidjavascript innerhtmljavascript calljavascript regexpjavascript includesjavascript thisjavascript eval