zoukankan      html  css  js  c++  java
  • 增强的对象字面量,解构赋值

    字面量(自变量)

         let name="wei";
    	let age=3;
    	let obj={
    		//简写变量,等同于name:name
    		name,
    		age
    	}
    		console.log(obj.name)//wei
    	let qqq = {
    	  name: 'wrs',
    	  toString () {  // 'function' keyword is omitted here
    	    return this.name;
    	  }
    	};
    
    
    	console.log(qqq.toString()); // wrs
    

    //通过对象字面量创建对象

    	var human = {
    	    breathe() {
    	        console.log('breathing...');
    	    }
    	};
    	var worker = {
    	    __proto__: human, //设置此对象的原型为human,相当于继承human
    	    company: 'freelancer',
    	    work() {
    	        console.log('working...');
    	    }
    	};
    	human.breathe();//输出 ‘breathing...’
    	//调用继承来的breathe方法
    	worker.breathe();//输出 ‘breathing...’
    

    解构赋值

    允许提取数组和对象里的值,赋给变量。
    function foo() {
      return [1,2,3];
    }
    let arr = foo(); // [1,2,3]
    
    let [a, b, c] = foo();
    console.log(a, b, c); // 1 2 3
    
    function bar() {
      return {
        x: 4,
        y: 5,
        z: 6
      };
    }
    let {x: x, y: y, z: z} = bar();
    console.log(x, y, z); // 4 5 6
  • 相关阅读:
    [LeetCode]Contains Duplicate
    C++基础之泛型算法
    KMP算法
    [LeetCode]Shortest Palindrome
    [LeetCode]House Robber
    Palindrome Linked List leetcode
    Rotate Array leetcode
    Rotate Image LeetCode
    Rotate List leetcode
    Reorder List leetcode
  • 原文地址:https://www.cnblogs.com/WWWrs/p/7152398.html
Copyright © 2011-2022 走看看