
批改狀態(tài):合格
老師批語:
值|引用傳遞-數(shù)組|對(duì)象解構(gòu)-call|apply|bind-訪問器-流程控制
- 實(shí)例演示值傳遞與引用傳遞的區(qū)別與聯(lián)系;
- 數(shù)組和對(duì)象解構(gòu)的常用方法與函數(shù)傳參
- call,apply,bind的區(qū)別與聯(lián)系,并試著實(shí)例演示一下
- 訪問器屬性的原理與實(shí)現(xiàn)過程,并實(shí)例演示;
- 多分支與switch轉(zhuǎn)換的技巧
- 三元運(yùn)算解決了什么問題,有什么限制?
// 值傳遞
let a = 1;
let b = a;
// a = 1, b = 1
console.log('a = %d, b = %d', a, b);
a = 2;
// 更新 a ,不影響 b 返回 a = 2, b = 1
console.log('a = %d, b = %d', a, b);
// 引用傳遞
a = {x: 1, y: 2};
b = a;
// {x: 1, y: 2} {x: 1, y: 2}
console.log(a, b);
a.x = 2;
// 更新對(duì)象 a ,對(duì)象 b 也更新
// {x: 2, y: 2} {x: 2, y: 2}
console.log(a, b);
// 數(shù)組解構(gòu)
let [x, y, z = 3] = [1, 2];
// 解構(gòu)到 x, y, z 變量中
// x = 1, y = 2, z = 3
console.log('x = %d, y = %d, z = %d', x, y, z);
// 對(duì)象解構(gòu)
let {x1, y1, z1 = 'three'} = {x1: 'one', y1: 'two'};
// 解構(gòu)到 x1, y1, z1 中
// x1 = one, y1 = two, z1 = three
console.log('x1 = %s, y1 = %s, z1 = %s', x1, y1, z1);
// 函數(shù)傳參
// 數(shù)組傳參
let linkArr = ([a, b, c]) => `${a} | ${b} | ${c}`;
// 1 | 2 | 3
console.log(linkArr([x, y, z]));
// 對(duì)象傳參
let linkObj = ({x1, y1, z1}) => `${x1} | ${y1} | ${z1}`;
// one | two | three
console.log(linkObj({x1, y1, z1}));
let test = function ( arg ) {
return arg;
};
const testObj = {
arg: 'args',
};
// 返回 Hi!
console.log(test('Hi!'));
// bind 返回一個(gè)函數(shù)聲明,不會(huì)立即執(zhí)行
let f1 = test.bind(testObj, 'bind');
// 返回 bind
console.log(f1());
// call | apply立即執(zhí)行
let f2 = test.call(testObj, 'call');
// 返回 call
console.log(f2);
// 返回 apply
let f3 = test.call(testObj, 'apply');
console.log(f3);
訪問器:將方法偽造成一個(gè)屬性,訪問器屬性優(yōu)先級(jí)高于同名的普通屬性
const result = {
data: {
id: 1,
meta: 'desc',
},
// 訪問器,方法偽造成一個(gè)屬性,取值
get getId() {
return this.data.id;
},
get getMeta() {
return this.data.meta;
},
// 訪問器,方法偽造成一個(gè)屬性,賦值
set setID(num) {
this.data.id = num;
},
set setMeta(value) {
this.data.meta = value;
},
}
// 訪問器,偽造的屬性賦值
result.setID = 2;
result.setMeta = 'test';
// 訪問器,偽造的屬性取值
// getID = 2, getMeta = test
console.log('getID = %s, getMeta = %s', result.getId, result.getMeta);
// If 多分支
let score = num => {
if (num >= 0 && num < 60) return '不及格';
else if (num >= 60 && num < 80) return '合格';
else if (num >=80 && num <= 100) return '優(yōu)秀';
else if (num < 0 || num > 100) return '非法';
}
// 返回:非法 不及格 合格 優(yōu)秀 非法
console.log(score(-1), score(50), score(60), score(100), score(101));
// switch來簡(jiǎn)化多分支
score = num => {
switch(true) {
case num >= 0 && num < 60:
return '不及格';
// return 無需 break;
case num >= 60 && num < 80:
return '合格';
case num >=80 && num <= 100:
return '優(yōu)秀';
default:
return '非法'
}
}
// 返回:非法 不及格 合格 優(yōu)秀 非法
console.log(score(-1), score(50), score(60), score(100), score(101));
// If 單值判斷
let response = status => {
if (status.toLowerCase() === 'fail') {
return '請(qǐng)求失?。?;
} else if (status.toLowerCase() === 'success') {
return '請(qǐng)求成功!';
} else {
return '非法請(qǐng)求!';
}
}
// 返回 請(qǐng)求成功!
console.log(response('SUCCESS'));
// switch 單值判斷
response = status => {
switch(status.toLowerCase()) {
case 'fail': return '請(qǐng)求失??!';
case 'success': return '請(qǐng)求成功!';
default: return '非法請(qǐng)求!';
}
}
// 返回 請(qǐng)求成功!
console.log(response('SUCCESS'));
let status = 'SUCCESS';
let res = status.toLowerCase() === 'fail' ? '請(qǐng)求失敗!' : (status.toLowerCase() === 'success' ? '請(qǐng)求成功!' : '非法請(qǐng)求!');
// 返回 請(qǐng)求成功!
console.log(res);
微信掃碼
關(guān)注PHP中文網(wǎng)服務(wù)號(hào)
QQ掃碼
加入技術(shù)交流群
Copyright 2014-2025 http://ipnx.cn/ All Rights Reserved | php.cn | 湘ICP備2023035733號(hào)