Update 2: This question is a mess, because I thought the ES6 class
doesn't modify .protototype, which it does, and hence this is exactly what I wanted.
I accepted the broadest answer even if all the answers and comments should have pointed me to the right direction in the very beginning :)
Thank you all!
Old:
In old JS, pre ES6, when we learned about making "classes" with:
function X() {
this.foo = function(){
}
};
var x = new X();
we also knew that every time we do x = new X();
we get a copy of the 'foo' method, in every instance This was one of the reasons why using prototype was a good idea.
Now, in ES6 we have this new cool class syntax, but with the same 'issue', i.e. here the 'f' method is copied. How do we avoid it in a ES6 way? Or do we still need to use .prototype?
class X{
f(){
return 'f';
}
}
console.clear();
let x1 = new X();
let x2 = new X();
x2.f = function() {return 'changed f';};
console.log(x1.f());//f
console.log(x2.f());//changed f
Update
I do understand we can still use .prototype. My question was more about using a more modern way to achieve what I wanted, i.e. not having copies of methods. I checked the very first tutorial about JS prototypes to find a citation, if my English is that bad :) - Found on https://hackernoon.com/prototypes-in-javascript-5bba2990e04b :
(...) i.e. every object created using the constructor function will have it’s own copy of properties and methods. It doesn’t make sense to have two instances of function fullName that do the same thing. Storing separate instances of function for each objects results into wastage of memory. We will see as we move forward how we can solve this issue.
And you also mentioned that class ...
is only a syntactic sugar, but then why... with function FC below, we can see can see the "f" method directly in fc1, and
function FC() {
this.v = 42;
this.f = function(){
}
};
var fc1 = new FC();
console.log('fc1, we can see the "f" directly in fc1: ',
Object.getOwnPropertyNames(fc1)
);
//////////////////////////////////
class X{
constructor(){
this.v = 'bar';
}
f(){
return 'f';
}
}
let x1 = new X();
console.log('x1, we don\'t, because we have it in prototype:',
Object.getOwnPropertyNames(x1)
);