var calNum = function(num){
this.num = num;
this.add = function(newNum) {
this.num += newNum;
};
this.sub = function(newNum) {
this.num -= newNum;
};
this.multi = function(newNum) {
this.num *= newNum;
};
this.division = function(newNum){
this.num /= newNum;
};
};
a.add(100);
console.log( a.num ); // 200
a.sub(50);
console.log( a.num ); // 150
//這邊呼叫兩次兩次才達到目的
class calNum {
constructor(num){
this.num= num ;
}
add(newNum) {
this.num += newNum;
return this;
}
sub(newNum) {
this.num -= newNum;
return this;
}
multi(newNum) {
this.num *= newNum;
return this;
}
division (newNum){
this.num /= newNum;
return this;
}
};
let a = new calNum(10)
a.add(10).sub(5).multi(5)
console.log(a)
//console出75
//這邊順便改成使用class的寫法