zoukankan      html  css  js  c++  java
  • [ES6] Objects create-shorthand && Destructuring

    Creating Object:

    Example 1:

    let name = "Brook";
    let totalReplies = 249;
    let avatar = "/users/avatars/brook-user-1.jpg";
    
    let user = {name, totalReplies, avatar};
    
    addUserToSidebar(user);

    Example 2:

    function buildMetadata(object){
      let id = parseInt(object.id);
      let lastUpdatedAt = object.updatedAt || object.createdAt;
      let hashCode = _buildHashCode(object);
      const isSecureHash = 32;
      
      
      return { 
        id, 
        lastUpdatedAt, 
        hashCode,
        isSecureHash() {
          return hashCode >= isSecureHash;
        }
      };
    }

    Example 3:

    function buildTopicElement(topic){
      let title = `<h2>${topic.title}</h2>`;
      let author = `<small>${topic.author}</small>`;
      let body = `<p>${topic.body}</p>`;
    
      return { title, author, body };
    }

    Object Destructuring:

    function buildMetadata(object){
      let id = parseInt(object.id);
      let lastUpdatedAt = object.updatedAt || object.createdAt;
      let hashCode = 16;
      const isSecureHash = 32;
      
      
      return { 
        id, 
        lastUpdatedAt, 
        hashCode,
        isSecureHash() {
          console.log("OK");
          return hashCode >= isSecureHash;
        }
      };
    }
    
    
    let id = 12,
        updatedAt: "Firday",
         obj = {id, updatedAt}
    
    let {isSecureHash} = buildMetadata(obj);
    isSecureHash();

    From the code, we can see, besides object, you can also Destructuring function from the object.

  • 相关阅读:
    wince5.0 key
    CSS基础知识思维导图xmind
    切图
    头部标签集
    List和Set的区别
    PicGo + Gitee 实现 Markdown 图床
    GitHub Pages 与 Gitee Pages 上的 Jekyll
    Agile, CI/CD,DevOps
    你还不了解DevOps? 看这篇就够了
    DevOps实践心得
  • 原文地址:https://www.cnblogs.com/Answer1215/p/5096707.html
Copyright © 2011-2022 走看看