ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

04.Javascript学习笔记3

2022-08-24 13:30:52  阅读:222  来源: 互联网

标签:mike console log 04 Javascript 笔记 数组 const friends


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'的引索
    

标签:mike,console,log,04,Javascript,笔记,数组,const,friends
来源: https://www.cnblogs.com/passion2021/p/16619559.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有