var n=3//基本型態在在裡面改了沒用,a=[3,2,1]//物件型態改了有用
function f5(f,a)
{
return f(a,5)
} //可以呼叫參數f
一個參數可以是一個函數,函數可以當參數
function df(f, x) {
var dx = 0.001;
var dy = f(x+dx) - f(x);
return dy/dx;
}
each[3,4,5,6] //印出3,4,5,6
class Complex {
constructor(r,i) {
this.r = r;
this.i = i;
}
add(c2) {
return new Complex(this.r+c2.r, this.i+c2.i);
}
sub(c2) {
return new Complex(this.r-c2.r, this.i-c2.i);
}
mul(c2) {
return new Complex(this.r*c2.r-this.i*c2.i,
this.r*c2.i+this.i*c2.r);
}
toString() {
return this.r+"+"+this.i+"i"
}
}
var a=new Complex(1,2), b=new Complex(2,1);
var x = a.add(b).sub(b).mul(b);
console.log("a=%s", a);
console.log("b=%s", b);
console.log("a.add(b)=%s", a.add(b));
console.log("a.sub(b)=%s", a.sub(b));
console.log("a.mul(b)=%s", a.mul(b));
console.log("x=%s", x);
home