object function筆記
const obj = {
value:'小明',
fn:function(){
return this.value
},
}
等同
const obj = {
value:'小明',
fn(){
return this.value
},
}
等同
function fn(){
return this.value
}
const obj = {
value:'小明',
fn,
}
呼叫function=> obj.fn()
擴展object需注意先解構再擴展,否則會影響原本的object
const obj = {
fn1(){
console.log(1);
},
fn2(){
console.log(2);
}
}
const newObj = {
...obj,
fn3(){
console.log(3)
}
}
解構筆記
const arr = [A,B,C]
const [A,B,C] = arr
const obj = {
A:'小明',
B:'小依'
}
const {A,B} = obj
//同名稱
const {A:newA,B:newB} = obj
//賦予名稱
const newObj = {
...new,
C:'小喵'
}
//不影響舊object的解構擴展