zoukankan      html  css  js  c++  java
  • ES6

    /**
     * 对象的扩展
     * 
     * 增强对象字面量
     * 
     * 解决问题:缩减代码
     */
    
    {
      /**
       * 1.属性简表示法
       * 变量foo直接写在大括号里面。这时,属性名就是变量名, 属性值就是变量值
       */
      const foo = 'bar';
      const baz = { foo };
      // baz // {foo: "bar"}
    
      // 等同于
      // const baz = { foo: foo };
    
    
      /**
       * 方法简写
       */
      const o1 = {
        method() {
          return "Hello!";
        }
      };
    
      // 等同于
    
      const o2 = {
        method: function () {
          return "Hello!";
        }
      };
    }
    
    {
      // 例子1
      let birth = '2000/01/01';
    
      const Person = {
    
        name: '张三',
    
        //等同于birth: birth
        birth,
    
        // 等同于hello: function ()...
        hello() { console.log('我的名字是', this.name); }
      };
    
    
    
      // 例子2
      function createBookShop(inventory) {
        return {
          inventory,    //属性简写  inventory:inventory,
          // inventoryValue: function () {
          inventoryValue() {
            return this.inventory.reduce((total, book) => total +
              book.price, 0);
          },
          //priceForTitle: function (title) {
          priceForTitle(title) {
            return this.inventory.find(book => book.title === title)
              .price;
          }
        }
      }
    
      const inventory = [
        { title: "Vue", price: 10 },
        { title: "Angular", price: 15 }
      ];
    
      const bookShop = createBookShop(inventory);
      console.log(bookShop.inventoryValue());         //25
      console.log(bookShop.priceForTitle("Angular")); //15
    
    }
    
    
    
  • 相关阅读:
    webpack打包报错configuration has an unknown property 'mode'
    CSP 201712-4 行车路线(最短路)
    设计模式
    sqlserver 迁移数据
    DataX
    Python 对接WebService
    IOS APP打包流程
    nginxUI
    ROS脚本-下线时判断在线数量进行重拨号
    bash 字符串截取的8种方法
  • 原文地址:https://www.cnblogs.com/tangge/p/12004047.html
Copyright © 2011-2022 走看看