JAVASCRIPT PROTOTYPE | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

JAVASCRIPT PROTOTYPE

I have seen people using prototypes in factory functions. And that seems really weird infact constructor functions and its prototypes look easier, but i was wondering should i avoid factory function and use constructor function prototype?(obj.prototype.prop/meth)...coz i was wondering which one will I use in job mostly?...thank you

2nd Jan 2019, 7:42 AM
Minggan
6 Answers
+ 2
https://www.sololearn.com/learn/JavaScript/2979/ Yes class is ES6 way (new way) you can use super() if you are extending class. If you are not extending class, just define the methods inside the class definition For function_name.prototype.method =... It is old way. Better compatibility. (means if the user is using old version browsers, they can still run this correctly)
2nd Jan 2019, 1:49 PM
Gordon
Gordon - avatar
+ 1
That's pre ES6 syntax and ES6 syntax Class constructor is ES6 syntax. Using prototype reserved word to declare prototypical function is pre ES6 way. Pre ES6 has better browser compatibility at user side. On the other hand, we use the improved syntax for new ECMA features when possible to reduce bugs and future proof our code. One more point to note, these browser incompatibilities don't apply to NodeJS applications.
2nd Jan 2019, 7:49 AM
Gordon
Gordon - avatar
+ 1
For backward compatibility, use Babel library of JS, then you can use ES6 syntax without worry that some visitors cannot run the Javascript correctly and view your intended effects : https://www.sololearn.com/Discuss/1220172/?ref=app
2nd Jan 2019, 2:22 PM
Gordon
Gordon - avatar
0
thnx bro but i am still bit confused u mean i should use the class constructor method? i mean using the consturctor and extend with super?
2nd Jan 2019, 11:43 AM
Minggan
0
If you like to learn from tutorial, this is one: https://www.sololearn.com/post/38381/?ref=app
2nd Jan 2019, 2:20 PM
Gordon
Gordon - avatar
0
thnx alot bro ur tha best
3rd Jan 2019, 7:04 AM
Minggan