zoukankan      html  css  js  c++  java
  • ES6——对象

    • 属性简洁表示法

      const age = 12
      const name = "Amy"
      const person = {age,name}
      
    • 方法名简写

      const person = {
          SayHi(){
              console.log("Hi")
          }
          //等同于
          //SayHi:function(){
             // console.log("Hi")
          //}
      }
      
      • Generator 函数

        • 简写

          const person = {
              * myGenerator(){
                  yield 'hello'
              }
          }
          
        • 概念

          异步编程解决方案 包含多个内部状态的状态机 返回一个遍历器对象

          function 关键字后面,函数名前面有一个符号 "*"

          内部用 yield 生产不同内部状态

          遍历器对象有个 next 方法,执行 next 会返回一个对象,对象上有两个属性 value:是yield 关键字后面表达式的值。 done:没有遇到 return 语句为 true ,遇到之后为 false

        • 基本使用

          function *helloGenerator(){
              yield 'hello'
              yield 'world'
              return 'ending'
          }
          var hw = helloGenerator()
          console.log(hw.next())	//{done: false, value: 'hello'}
          console.log(hw.next())	//{done: false, value: 'world'}
          console.log(hw.next())	//{done: true, value: 'ending'}
          console.log(hw.next())	//{done: true, value: undefined}
          console.log(hw.next())	//{done: true, value: undefined}
          
    • 属性名表达式

      const obj = {
          ["he"+"llo"](){			//表达式一定要放在方框内
              return 'Hi'
          }
      }
      obj.hello()		//"Hi"
      

      :属性简洁表示法不能与属性表达式同时使用

    • 对象拓展运算符

      取出参数对象所有可遍历属性拷贝到当前对象

      let person = {name:"Amy", age: 15}
      let someone = {...person}
      someone		//{name: "Amy", age: 15}
      

      也可用于合并

      let age = {age: 15}
      let name = {name: "Tom"}
      let person = {...age, ...name}
      person		//{age: 15, name: 'Tom'}
      

      自定义属性和拓展运算符对象里面的属性的时候,拓展运算符优先级 > 自定义属性

      如果拓展运算符是空对象或 null, undefined 则使用自定义属性

    • 对象新方法

      • Object.assign(target,source,...)

        将源对象所有可枚举属性复制到目标对象中

        let target = {a: 1}
        let obj2 = {b: 2}
        let obj3 = {c: 3}
        Object.assign(target,obj2,obj3)
        target	//{a: 1, b:2, c:3}
        
        1. 如果一个目标对象和源对象有同名属性,后面属性覆盖前面属性
        2. 如果参数不是对象则转化为对象返回
        3. assign 是浅拷贝
      • Object.is(value1, value2)

        比较两个值是否严格相等,与(===)相似

        • 与(===)的不同

          NaN === NaN	//false
          Object.is(NaN,NaN)	//true
          
          +0 === -0	//true
          Object.is(+0,-0)	//false
          
  • 相关阅读:
    win10 uwp 弹起键盘不隐藏界面元素
    win10 uwp 存放网络图片到本地
    win10 uwp 存放网络图片到本地
    sublime Text 正则替换
    sublime Text 正则替换
    win10 uwp 绘图 Line 控件使用
    win10 uwp 绘图 Line 控件使用
    AJAX 是什么?
    什么是 PHP SimpleXML?
    PHP XML DOM:DOM 是什么?
  • 原文地址:https://www.cnblogs.com/angle-yan/p/13385241.html
Copyright © 2011-2022 走看看