ES5ä¸çæ°å¢æ¹æ³
1.1ES5æ°å¢æ¹æ³æ¦è¿°
ES5ä¸ç»æ们æ°å¢äºä¸äºæ¹æ³ï¼å¯ä»¥å¾æ¹ä¾¿çæä½æ°ç»æè
å符串ï¼è¿äºæ¹æ³ä¸»è¦å
æ¬ï¼
â æ°ç»æ¹æ³
â å符串æ¹æ³
â 对象æ¹æ³
1.2 æ°ç»æ¹æ³
è¿ä»£ï¼éåï¼æ¹æ³ï¼forEach()ãmap()ãfilter()ãsome()ãevery();
forEach()
array.forEach(function(currentValue,index,arr))
â currentValueï¼æ°ç»å½å项çå¼
â indexï¼æ°ç»å½å项çç´¢å¼
â arrï¼æ°ç»å¯¹è±¡æ¬èº«
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<script>
var arr = [1, 2, 3];
var sum = 0;
arr.forEach(function(value, index, array) {
console.log('æ¯ä¸ªæ°ç»å
ç´ ' + value);
console.log('æ¯ä¸ªæ°ç»å
ç´ çç´¢å¼å·' + index);
console.log('æ°ç»æ¬èº«' + array);
sum += value;
})
console.log(sum);
</script>
</body>
</html>
filter()
array.filter(function(currenValue, index, arr))
â filter()æ¹æ³å建ä¸ä¸ªæ°çæ°ç»ï¼æ°æ°ç»ä¸çå
ç´ æ¯éè¿æ£æ¥æå®æ°ç»ä¸ç¬¦åæ¡ä»¶çææå
ç´ ï¼ä¸»è¦ç¨äºçéæ°ç»
â 注æå®ç´æ¥è¿åä¸ä¸ªæ°æ°ç»
â currenValueï¼æ°ç»å½å项çå¼
â indexï¼æ°ç»å½å项çç´¢å¼
â arrï¼æ°ç»å¯¹è±¡æ¬èº«
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<script>
//filter çéæ°ç»
var arr = [12, 66, 4, 88, 3, 7];
var newArr = arr.filter(function(value, index) {
// return value >= 20;
//æ±æ°ç»éé¢çææå¶æ°
return value % 2 ===0;
})
console.log(newArr); //è¾åºç»æ66,88
console.log(newArr); //è¾åºç»æ12,66,4ï¼88
</script>
</body>
</html>
some()
array.some(function(currentValue, index, arr))
â some()æ¹æ³ç¨äºæ£æµæ°ç»ä¸çå
ç´ æ¯å¦æ»¡è¶³æå®æ¡ä»¶ï¼éä¿ç¹æ¥æ¾æ°ç»ä¸æ¯å¦æ满足æ¡ä»¶çå
ç´
â 注æå®è¿åå¼æ¯å¸å°å¼ï¼å¦ææ¥æ¾å°è¿ä¸ªå
ç´ ï¼å°±è¿åtrueï¼å¦ææ¥æ¾ä¸å°å°±è¿åfalse
â å¦ææ¾å°ç¬¬ä¸ä¸ªæ»¡è¶³æ¡ä»¶çå
ç´ ï¼åç»æ¢å¾ªç¯ï¼ä¸å¨ç»§ç»æ¥æ¾
â currentValueï¼æ°ç»å½å项çå¼
â indexï¼æ°ç»å½å项çç´¢å¼
â arrï¼æ°ç»å¯¹è±¡æ¬èº«
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<script>
//someæ¥æ¾æ°ç»ä¸æ¯å¦æ满足æ¡ä»¶çå
ç´
var arr = [10, 30, 4];
var flag = arr.some(function(value, index) {
return value >= 20;
})
console.log(flag);
</script>
</body>
</html>
1ãfilterä¹æ¯æ¥æ¾æ»¡è¶³æ¡ä»¶çå
ç´ ï¼è¿åçæ¯ä¸ä¸ªæ°ç»ï¼èä¸æ¯æææ满足æ¡ä»¶çå
ç´ è¿ååæ¥
2ãsome ä¹æ¯æ¥æ¾æ»¡è¶³æ¡ä»¶çå
ç´ æ¯å¦åå¨ï¼è¿åçæ¯ä¸ä¸ªå¸å°å¼ï¼å¦ææ¥æ¾å°ç¬¬ä¸ä¸ªæ»¡è¶³æ¡ä»¶çå
ç´ å°±ç»å¼å¾ªç¯
1.3 å符串æ¹æ³
trim()æ¹æ³ä¼ä»ç»ä¸ä¸ªå符串ç俩端å é¤ç©ºç½å符
str.trim()
trim()æ¹æ³å¹¶ä¸å½±ååå符串æ¬èº«ï¼å®è¿åçæ¯ä¸ä¸ªæ°çå符串
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<script>
//trim()å»é¤ä¿©ä¾§ç©ºå符串
var str = ' andy '
console.log(str);
var str1 = str.trim();
console.log(str1);
</script>
</body>
</html>
1.4 对象æ¹æ³
1ãObject.key()ç¨äºè·å对象èªèº«ææçå±æ§
Object.key(obj)
â ææ类似forâ¦in
â è¿åä¸ä¸ªç±å±æ§åç»æçæ°ç»
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<script>
</script>
</body>
<script>
//è·å对象èªèº«ææçå±æ§
var obj = {
id: 1,
pname: 'å°ç±³',
price: 3999
};
var arr = Object.keys(obj);
console.log(arr);
</script>
</html>
2ãObject.defineProperty()å®ä¹å¯¹è±¡ä¸æ°å±æ§æä¿®æ¹åæçå±æ§
Object.defineProperty(obj, prop, descriptor)
â objï¼å¿
éãç®æ 对象
â propï¼å¿
éãéå®ä¹æä¿®æ¹çå±æ§çåå
â descriptorï¼å¿
éãç®æ å±æ§ææ¥æçç¹æ§
ObjectãdefineProperty()第ä¸ä¸ªåæ°descriptor说æï¼ä»¥å¯¹è±¡å½¢å¼{}书å
â valueï¼è®¾ç½®å±æ§çå¼ é»è®¤undefined
â writableï¼å¼æ¯å¦å¯ä»¥éåãtrue | false é»è®¤ä¸ºfalse
â enumerableï¼ç®æ å±æ§æ¯å¦å¯ä»¥è¢«æ举ãtrue|falseé»è®¤ä¸ºfalse
â configurableï¼ç®æ å±æ§æ¯å¦å¯ä»¥è¢«å é¤ææ¯å¦å¯ä»¥å次修æ¹ç¹æ§true|falseé»è®¤ä¸ºfalse
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<script>
</script>
</body>
<script>
//è·å对象èªèº«ææçå±æ§
var obj = {
id: 1,
pname: 'å°ç±³',
price: 3999
};
// var arr = Object.keys(obj);
// console.log(arr);
//Object.defineProperty()å®ä¹æ°å±æ§æä¿®æ¹åæçé¢å±æ§
Object.defineProperty(obj, 'num', {
value: 1000
});
Object.defineProperty(obj, 'price', {
value: 9.9
});
Object.defineProperty(obj, 'price', {
//å¦æfalseï¼ä¸å
许修æ¹å±æ§å¼ï¼é»è®¤å¼ä¸ºfalse
writable: false,
});
</script>
</html>