const x = new Calc
function Car (name){
this.name = name
}
Car.prototype.sound = function(){
console.log("vroom");
}
// class syntax
class Car {
constructor(name){
this.name = name;
}
sound(){
console.log("vroom");
}
}
constructor()
function but you arent instantiating the class, so shouldnt you use static
class field instead?
static
fields to the class, I think it'd be better for your use case:class Calc {
static sum;
static operatorMemory;
static reset() {
Calc.sum = undefined;
Calc.operatorMemory = undefined;
}
...
Can you also tell me the difference between using
class Calc {
static sum;
static operatorMemory;
static reset() {
Calc.sum = undefined;
Calc.operatorMemory = undefined;
}
}
and using a object like this
const calc = {
sum,
operatorMemory,
reset: function reset() {
this.sum = undefined;
this.operatorMemory = undefined;
}
}
I am confused when to use which
new
one, you are using class/static methods, whereas when you create a new instance you have access to its prototype/instance methods.