function
使用老師gitlab程式碼
// 第一種寫法,直接宣告函數
function sub(a,b) {
return a-b;
}
// 第二種寫法,將匿名函數指定給變數。
var add = function(a,b) {
return a+b;
}
console.log("add(3,5)=", add(3,5), " sub(7,2)=", sub(7,2));
-->
add(3,5)= 8 sub(7,2)= 5
max(function)
function max(a,b) {
if (a>b)
return a;
else
return b;
}
var m = max(9,5);
console.log("max(9,5)="+m);
-->
max(9,5)=9
sum(function)
function sum(n) {
var i=1, s=0;
while (i<=n) {
s = s+i;
console.log('i=', i, 's=', s)
i = i+1;
}
return s;
}
var s5=sum(5)
console.log('sum(5)='+s5)
-->
i= 1 s= 1
i= 2 s= 3
i= 3 s= 6
i= 4 s= 10
i= 5 s= 15
sum(5)=15
modify(function)
function modify(num, array) {
num = num + 1;
array[0] = array[0] + 1;
}
var n=3, a=[3,2,1];
modify(n, a);
console.log("n=", n, " a=", a);
-->
n= 3 a= [ 4, 2, 1 ]
fptr
function sub(a,b) {
return a-b;
}
function f5(f, a) {
return f(a, 5);
}
console.log("sub(8,5)="+sub(8, 5));
console.log("f5(sub,8)="+f5(sub,8));
-->
sub(8,5)=3
f5(sub,8)=3
微積分(微分)
function df(f, x) {
var dx = 0.001;
var dy = f(x+dx) - f(x);
return dy/dx;
}
function square(x) {
return x*x;
}
console.log('df(x^2,2)='+df(square, 2));
console.log('df(x^2,2)='+df(function(x){ return x*x; }, 2));
console.log('df(sin(x),pi/4)='+df(Math.sin, 3.14159/4));
-->
df(x^2,2)=4.000999999999699
df(x^2,2)=4.000999999999699
df(sin(x),pi/4)=0.7067535793015001
微積分(積分)
function integral(f, a, b) {
var dx = 0.001;
var area = 0.0;
for (var x=a; x<b; x=x+dx) {
area = area + f(x)*dx;
}
return area;
}
function square(x) {
return x*x;
}
console.log('integral(x^2,0,1)='+integral(square,0,1));
console.log('integral(sin(x),0,pi)='+integral(Math.sin,0,3.14159));
-->
integral(x^2,0,1)=0.33283350000000095
integral(sin(x),0,pi)=1.9999999540411524
map
var c = console;
function map(f, a) {
var ma = [];
for (var i in a) {
ma.push(f(a[i]));
}
return ma;
}
function square(x) {
return x*x;
}
c.log("map(x^2, [3, 1, 5, 4, 2])="+map(square, [3,1,5,4,2]));
c.log("map(sin(x), [3, 1, 5, 4, 2])="+map(Math.sin, [3,1,5,4,2]));
-->
map(x^2, [3, 1, 5, 4, 2])=9,1,25,16,4
map(sin(x), [3, 1, 5, 4, 2])=0.1411200080598672,0.8414709848078965,-0.9589242746631385,-0.7568024953079282,0.9092974268256817
run
function run(f, x) {
var fx = f(x);
console.log("f(x)="+fx);
}
run(Math.sin, Math.PI/4);
-->
f(x)=0.7071067811865475
物件範例
ccc = {name:'ccc',age:51}
-->{name: "ccc", age: 51}
ccc
-->{name: "ccc", age: 51}
ccc.age
-->51
ccc.name
-->"ccc"
snoopy={}
-->{}
snoopy.name='史努比'
-->"史努比"
snoopy.friend=[]
-->[]
snoopy
-->{name: "史努比", age: 3, friend: Array(0)}
snoopy.friend[0]=ccc
-->{name: "ccc", age: 51}
age: 51
name: "ccc"
c2e(json)
var c2e = { '狗': 'dog' , '貓':'cat', '一隻': 'a', '這隻': 'the', '追': 'chase', '吃': 'eat' }
function mt (c) {
var e = []
for (let i in c) {
var cword = c[i]
var eword = c2e[cword]
e.push(eword)
}
return e
}
console.log('Deno.args=', Deno.args)
var e = mt(Deno.args)
console.log(e)
-->
(PS C:\code\wp\03-js\06-json> deno run c2e.js 一隻 狗 追 一隻 貓)
Deno.args= [ "一隻", "狗", "追", "一隻", "貓" ]
[ "a", "dog", "chase", "a", "cat" ]
e2c(json)
var e2c = { dog: '狗', cat: '貓', a: '一隻', the: '這隻', chase: '追', eat: '吃' }
function mt (e) {
var c = []
for (let i in e) {
var eword = e[i]
var cword = e2c[eword]
c.push(cword)
}
return c
}
console.log('Deno.args=', Deno.args)
var c = mt(Deno.args)
console.log(c)
-->
(PS C:\code\wp\03-js\06-json> deno run e2c.js a dog chase a cat )
Deno.args= [ "a", "dog", "chase", "a", "cat" ]
[ "一隻", "狗", "追", "一隻", "貓" ]