发布于2022年10月15日2年前 1.箭头函数 箭头函数是一种更短的函数表达式。 const age = birthyear => 2022 - birthyear; console.log(age(2000)) 箭头左边的birthyear是参数,箭头右边是要执行的代码块。在编写如上单行函数时,我们不需要写花括号,也不需要写return关键字,但实际上这些都是隐式发生的。 多行函数的情况:使用花括号 ' { } ' const years = birthyear => { const age = 2022 - birthyear; const retirement = 65 - age; return retirement; } console.log(years(2000)); 也可以使用多个参数:(brithyear,name) const your_age = (brithyear,name) => { const age = 2022 - brithyear; return `${name},you are ${age} years old `; } console.log(your_age(2000,'soria')); 2. 数组 构造一个数组 const friends = ['mike','adams','pat']; console.log(friends); 使用Arroy函数构造数组 const years = new Array(1991,1984,2008,2020); console.log(years); 注意Array的 ' A ' 要大写,array前面要加上new关键字。 查看数组的长度 const friends = ['mike','adams','pat']; console.log(friends.length); 数组的引索 const friends = ['mike','adams','pat']; console.log(friends[0],friends[1],friends[2],friends[3]); 更改数组里的元素 const friends = ['mike','adams','pat']; friends[2] = 'jay'; console.log(friends); 数组的运算 console.log(2037 - [1990,1967]); console.log(Number([1990,1967]));// 数组强制转换为数字类型 结果为NaN console.log(2037 + [1990,1967]); // 数组强制转换为字符串 3. 数组的方法 push函数 const friends = ['mike','adams','pat']; const newlength = friends.push('jay'); //push在数组末尾添加值'jay' , 同时可以返回新数组的长度 console.log(friends); console.log(newlength); unshift函数 const friends = ['mike','adams','pat']; const newlength = friends.unshift('jay'); //unshift在数组开头添加值'jay' , 同时可以返回新数组的长度 console.log(friends); console.log(newlength); pop函数 const friends = ['mike','adams','pat']; friends.pop(); console.log(friends); const popped = friends.pop();// 删除数组末尾的值'adams',同时返回这个值 console.log(popped); console.log(friends); include函数 const friends = ['mike','adams','pat']; console.log(friends.includes('mike')) // 查找数组里是否含有'mike',返回一个bool值 if (friends.includes('mike')){ console.log('you have a friend called mike') } indexOf函数 const friends = ['mike','adams','pat']; console.log(friends.indexOf('mike')) // 返回'mike'的引索
创建帐户或登录后发表意见