首页 > 其他 > 详细

[ES6] Objects create-shorthand && Destructuring

时间:2016-01-03 18:16:23      阅读:156      评论:0      收藏:0      [点我收藏+]

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.

[ES6] Objects create-shorthand && Destructuring

原文:http://www.cnblogs.com/Answer1215/p/5096707.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!