[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.

时间: 2024-10-13 00:55:25

[ES6] Objects create-shorthand && Destructuring的相关文章

[ES6] Objects vs Maps

Map is really useful when you want to use object as a key to set vaule, in ES5, you cannot really use object as a key: var user1 = { name: "Wan", age: 25 }; var user2 = { name: "Zhen", age: 27 }; var users = {}; users[user1] = 5; users

[ES6] 09. Destructuring Assignment -- 2

Read More: http://es6.ruanyifeng.com/#docs/destructuring Array “模式匹配”,只要等号两边的模式相同,左边的变量就会被赋予对应的值: Exp 1: var [head, ...tail] = [1, 2, 3, 4]; console.log(head); // 1 console.log(tail); // [2, 3, 4] Exp 2: var [foo, [[bar], baz]] = [1, [[2], 3]]; foo /

ES6 Syntax and Feature Overview(待续)

View on GitHub Note: A commonly accepted practice is to use const except in cases of loops and reassignment. However, in this resource I'll be using let in place of var for all ES6 examples. Variable: x Object: obj Array: arr Function: func Parameter

ES6学习笔记(一)

1.ES6学习之let.const (1).var.let.const 变(常)量声明 ES5 只有全局作用域和函数作用域,没有块级作用域,这带来很多不合理的场景. 在ES6中let就诞生了,实际上它为javascript新增了块级作用域. let只在变量声明时 ,所在的代码块中有效. 由于这一特性,let很适合在for循环中使用. 如下图是一个demo: let声明变量的特点: a:不存在变量提升: console.log(m);//Uncaught ReferenceError: m is

ES6之主要知识点(二) 变量的解构赋值。默认值

引自http://es6.ruanyifeng.com/#docs/destructuring 数组解构赋值 默认值 对象解构赋值 用途 1.数组的解构赋值 let [a, b, c] = [1, 2, 3]; let [foo, [[bar], baz]] = [1, [[2], 3]]; foo // 1 bar // 2 baz // 3 let [ , , third] = ["foo", "bar", "baz"]; third //

了解ES6

内容: 1.ES6介绍及基础 2.模块.类和继承 3.ES6高级特性 4.Generator和Iterator 5.异步编程 内容参考:<ES6 标准入门> ES6标准阅读链接:http://es6.ruanyifeng.com/ 一.ES6介绍及基础 1.什么是ES6 ECMAScript 6.0(以下简称 ES6)是 JavaScript 语言的下一代标准,已经在 2015 年 6 月正式发布了.它的目标,是使得 JavaScript 语言可以用来编写复杂的大型应用程序,成为企业级开发语言

django的queryset和objects对象

1. queryset是查询集,就是传到服务器上的url里面的内容.Django会对查询返回的结果集QerySet进行缓存,这里是为了提高查询效率. 也就是说,在你创建一个QuerySet对象的时候,Django并不会立即向数据库发出查询命令,只有在你需要用到这个QuerySet的时候才回去数据库查询. 2. Objects是django实现的mvc框架中的数据层(model)m,django中的模型类都有一个objects对象,它是一个django中定义的QuerySet类型的对象, 它包含了

ES6 - ECMAScript 6.0(ES2015)

# ES6 - ECMAScript 6.0(ES2015) ## 变量声明- let.const ### let - ES6 新增了let命令,用来声明变量.它的用法类似于var,但是所声明的变量,只在let命令所在的代码块内有效. - `let`特点: - 1 块级作用域(ES6) - 2 先声明再使用 - 3 不允许重复声明 ```js/* 基本使用 */let num = 2 { let num = 9}console.log(num)``` ### const - const声明一个*

django-改写manage类-objects

user/models.py中 class AddressManage(models.Manager): '''地址模型管理类''' def get_default_addr(self, user): '''获取默认收货地址''' try: address = self.get(user=user, is_default=True) except self.model.DoesNotExist: # 不存在默认收货地址 address = None return address class Ad