• JavaScript学习笔记[0]


    JavaScript学习笔记[0]

    数据类型

    • Number 表示数字,不区分浮点整形。
    • === 比较时不转化数据类型。 == 反之。
    • NaN与任何值都不想等,包括自己。只能用isNaN()
    isNaN(NaN);   //true
    
    • 浮点数计算有误差。任何语言都是这样。只能通过计算差值来确定。
    1 / 3 === (1 - 2 / 3) //false
    
    Math.abs(1 / 3 - (1 - 2 / 3)) < 0.0000001 //true
    
    • null 和 undefined 类似
    • 对字符串索引赋值,不会改变字符串。字符串的方法都是返回一个新的字符串。
    • 数组。unshift() 往数组头部田间。shift()删除第一个头部元素。与push(),pop()对应。
    • arr.splice()修改数组的万能方法。splice()
    • arr.concat()连接数组。
    • arr.join()把把所有元素用指定字符串连接。返回结果字符串。
    var arr = ['A','B','C',1,2,3];
    arr.join('-'); //'A-B-C-1-2-3'
    
    • for ... of ... 访问Map和Set。

    函数

    • 要求传入参数却没有传入。返回NaN;
    • arguments 获取所有参数
    • 用好typeof,用于验证参数的传入的合法性。
    • for循环内的局部变量,用let
    • 修复this的指向。apply()参数打包成数组,call()按参数顺序传入。
    • array的map()方法,reduce()

    [x1, x2, x3, x4].reduce(f) = f(f(f(x1, x2), x3), x4)

    • filter(pow) 传入一个bool函数。过滤元素。巧妙地用filter()回调函数去除array重复元素。
    var
        r,
        arr = ['apple', 'strawberry', 'banana', 'pear', 'apple', 'orange', 'orange', 'strawberry'];
    r = arr.filter(function (element, index, self) {
        return self.indexOf(element) === index;
    });
    
    • sort()函数按默认转化字符串排序。排序不改变array,返回新的array。
    • 闭包的作用。阮一峰的博客讲的很新手一些吧。维基上关于闭包的解释,我感觉还是比较抽象的。
    • 箭头函数。简化了函数定义,解决部分this指针问题。
    • generator. yield;

    function*()

    标准对象

    • typeof
    typeof 123; // 'number'
    typeof NaN; // 'number'
    typeof 'str'; // 'string'
    typeof true; // 'boolean'
    typeof undefined; // 'undefined'
    typeof Math.abs; // 'function'
    typeof null; // 'object'
    typeof []; // 'object'
    typeof {}; // 'object'
    
    • 小心使用对象

      • 不要使用new Number()、new Boolean()、new String()创建包装对象;

      • 用parseInt()或parseFloat()来转换任意类型到number;

      • 用String()来转换任意类型到string,或者直接调用某个对象的toString()方法;

      • 通常不必把任意类型转换为boolean再判断,因为可以直接写if (myVar) {...};

      • typeof操作符可以判断出number、boolean、string、function和undefined;

      • 判断Array要使用Array.isArray(arr);

      • 判断null请使用myVar === null;

      • 判断某个全局变量是否存在用typeof window.myVar === 'undefined';

      • 函数内部判断某个变量是否存在用typeof myVar === 'undefined'。

    • Date

    var now = new Date();
    now; // Wed Jun 24 2015 19:49:22 GMT+0800 (CST)
    now.getFullYear(); // 2015, 年份
    now.getMonth(); // 5, 月份,注意月份范围是0~11,5表示六月
    now.getDate(); // 24, 表示24号
    now.getDay(); // 3, 表示星期三
    now.getHours(); // 19, 24小时制
    now.getMinutes(); // 49, 分钟
    now.getSeconds(); // 22, 秒
    now.getMilliseconds(); // 875, 毫秒数
    now.getTime(); // 1435146562875, 以number形式表示的时间戳
    
    • getMonth() 0~11

    • RegExp

      • d 匹配数字
      • w 匹配字母或数字
      • . 匹配任意字符
        • 表示任意字符,包括0个
        • 表示一个字符
      • ? 表示0个或1个
      • {n} 表示n个字符,{n,m} 表示n-m个字符
      • ^ 行的开头, $ 行的结束
      • 更多具体可见正则大全
    var s = 'JavaScript, VBScript, JScript and ECMAScript';
    var re=/[a-zA-Z]+Script/g;
    
    // 使用全局匹配:
    re.exec(s); // ['JavaScript']
    re.lastIndex; // 10
    
    re.exec(s); // ['VBScript']
    re.lastIndex; // 20
    
    re.exec(s); // ['JScript']
    re.lastIndex; // 29
    
    re.exec(s); // ['ECMAScript']
    re.lastIndex; // 44
    
    re.exec(s); // null,直到结束仍没有匹配到
    
    • JSON
  • 相关阅读:
    记录s标签范例
    链表问题总结
    Hibernate学习总结
    HDU2460-Network
    CF464C-Substitutes in Number
    CF666E-Forensic Examination
    CF373C-Counting Kangaroos is Fun
    CF558E-A Simple Task
    HDU5669-Road
    CF341D-Iahub and Xors
  • 原文地址:https://www.cnblogs.com/weedboy/p/7401501.html
Copyright © 2020-2023  润新知