What is prototype syntax
Prototype syntax is still useful.
Before ECMAscript 6 the class keyword did not exist so developers had to use prototype syntax:
function Name(params) {
this.name = value;
// ...
}
ClassName.prototype.name = function (params) {
// ...
}
// ...
That was prototype syntax.
why is it still useful
If you are using a legacy environment where ECMAscript 6 doesn't exist. That's when prototype syntax becomes useful.
Prototype syntax is in commonJS so you can use this in CJS(CommonJS) scripts.
NOTE: modern CommonJS supports class so prototype syntax is still legacy by definition
Why class exists
class exists to make OOP(Object Oriented Programming) easier.
Protoype syntax altho useful, is more complex and lengthy to use. even with minification
it also introduced the constructor keyword
classes introduced getters and setters
with the new class syntax you can now use:
class name {
constructor(params) {
this.name = value;
// ...
}
get value() {
// ...
}
set value() {
// ...
}
// ...
}
Most IDEs globally support class so its still recommended if you dont have a valid reason to use prototype syntax: use class.
Closing line
This is part 1 of the legacy JS code is useful series.
Top comments (0)