zoukankan      html  css  js  c++  java
  • 高级H5游戏开发(Advanced game design with html5 and javascript )读书学习笔记(1)--es6相关

    第一章都是es6相关的基础知识,当复习巩固了

               

    es6:
    1.let变量有了块作用域
    2.箭头函数,this指针
       this指针固定,没有绑定上下文的问题,不需要用var self = this。
    3.遍历数组
       1.最普通的遍历方法 for(let i = 0 ; j = arr.length ; i<j ; i++)
        2.forEach((value,index,array)=>{})   forEach后两个参数可选
        3.for循环遍历 for(let item in array)
    4.遍历对象
      遍历属性key:
      1.
      Object.keys(anyObject)可以返回所有keys的一个数组
      Object.getOwnPropertyNames可以遍历有隐藏属性(该对象称为none-numerable)的对象。
      room.hasOwnProperty检查对象是否含某个属性
      2.
      for( let key in object)
     注意:这种方式遍历key没有顺序,而且它会去原型链上找,所以可以用hasOwnProperty()判断该对象是否有此属性
    5,数组处理的一些Es6方法
     some()和every() 遍历,回调里一个是true,一个是false返回
     find()找对应元素,findIndex返回下标
    map(),filter()等等,简化以前的for循环,其实都可以实现同样效果。
    reduce()求和很方便。total = numbers.reduce((a, b) => a + b,initValue);
    6.数组解构
    一种匹配模式,可以方便定义变量
         let statistics = [16, 170, 10];
         let [age, height, grade] = statistics;
        注意各种匹配情况
    7.string 模版
      以前要用+去连接字符串,现在可以如下
     `This is a template string that displays a ${variableName}`
       注意和单引号区分
    8.函数参数
        es6可以给参数默认值:
         function display(name = "rose", color = "red") {}
            …arguments,用来定义不确定个数的参数:
               function displayColors(...colorArray) {} 
               displayColors("red", "green", "blue”);
    9.geters和seters
       和其他一些面向对象语言类似的概念,seter方法可以用来控制,按条件来设置对象属性,geter用来获取对象属性
    10.Object.defineProperty用来定义不可变属性,

      data descriptor :

    //1. Create a jar object
    let jar = {};
    //2. Use Object.defineProperty to create a property called
    //cookies in the jar object. Set its value and other properties
    //that determine how it can be viewed or changed
    Object.defineProperty(jar, "cookies", {
      value: 10,
      writeable: false,
      enumerable: true,
      configurable: true
    });

       accessor descriptor :

    //1. Create a jar object
    let jar = {};
    //2. Use Object.defineProperty to create a property called
    //cookies in the jar object. Create a getter and setter so
    //that its value can be changed
    Object.defineProperty(jar, "cookies", {
      get() {
        return this.value;
      },
    set(newValue) {
        this.value = newValue;
      },
      enumerable: true,
      configurable: true
    });
    //3. Give the new property an initial value
    
    jar.cookies = 10;

      定义多个用 Object.defineProperties

    var jar = {};
    Object.defineProperties(jar, {
      "cookies": {
        value: 10,
        writable: true,
        enumerable: true,
        configurable: true
    },
      "lid": {
        value: "closed",
        writable: false,
        enumerable: true,
        configurable: true
    } });

     Object.assign(newObject, config) //合并对象,属性覆盖

    11.class  语法糖,有constructor,可以new对象, 其实还是基于原型链。

  • 相关阅读:
    ansible笔记(三)--模块讲解
    Linux 后台执行top 出错“TERM environment variable not set” “top: failed tty get”
    信息熵与TF-IDF 学习笔记
    大数运算 文献收藏
    树状数组 Binary Indexed Tree 学习笔记
    Linux Apache 安装(无外网)
    大数定律与中心极限定理
    Python 如何同时遍历两个文件
    监督学习模型分类 生成模型vs判别模型 概率模型vs非概率模型 参数模型vs非参数模型
    unittest生成测试报告
  • 原文地址:https://www.cnblogs.com/johnzhu/p/6797135.html
Copyright © 2011-2022 走看看