week10.md - mozi5269/wp109b GitHub Wiki
使用老師gitlab程式碼
dlook
var e2c = { dog: '狗', cat: '貓', a: '一隻', chase: '追', eat: '吃' };
function look (e) {
return e2c[e]
}
console.log('args=', Deno.args)
let e = Deno.args[0] // 從 process.argv 這個陣列取出第二個元素
let c = look(e)
console.log(e + '=' + c)
-->
(PS C:\code\wp\03-js\06-json> deno run dlook.js dog)
args= [ "dog" ]
dog=狗
json
var george = {
'name': 'George',
'age': 25,
'friends': [
{ 'name': 'John', 'age': 22 },
{ 'name': 'Mary', 'age': 28 }
]
}
console.log('george = ', george)
console.log('george.age=' + george.age)
console.log('george.friends:')
var friends = george.friends
for (let i in friends) {
console.log(' ' + friends[i].name + ' is ' + friends[i].age + ' years old!')
}
-->
george = {
name: "George",
age: 25,
friends: [ { name: "John", age: 22 }, { name: "Mary", age: 28 } ]
}
george.age=25
george.friends:
John is 22 years old!
Mary is 28 years old!
circle
var circle = {
r:3,
area:function() {
return 3.14*this.r*this.r;
}
}
console.log("circle.r=", circle.r);
console.log("circle.area()=", circle.area());
-->
circle.r= 3
circle.area()= 28.259999999999998
circleClass
class Circle {
constructor(r) {
this.r = r
}
area() {
return 3.14*this.r*this.r;
}
}
var c1 = new Circle(1)
console.log("c1.r=", c1.r);
console.log("c1.area()=", c1.area());
var c2 = new Circle(2)
console.log("c2.area()=", c2.area());
-->
c1.r= 1
c1.area()= 3.14
c2.area()= 12.56