Skip to Content
React学习手册
book

React学习手册

by Alex Banks, Eve Porcello
December 2017
Intermediate to advanced
329 pages
6h 9m
Chinese
China Electric Power Press Ltd.
Content preview from React学习手册
24
2
本章后续的内容中,将会结合数组解构和扩展运算符进一步拓展这个示例。
对象语义增强
对象语义增强和解构恰恰相反。它是重组或者回炉再造的过程。通过对象语义增强,
我们可以从全局作用域中获得变量并将它们转换成一个对象:
var name = "Tallac"
var elevation = 9738
var funHike = {name,elevation}
console.log(funHike) // {name: "Tallac", elevation: 9738}
name
elevation
现在变成
funHike
对象的属性键了。
我们还可以通过对象语义增强或重组构造对象的方法:
var name = "Tallac"
var elevation = 9738
var print = function() {
console.log(`Mt. ${this.name} is ${this.elevation} feet tall`)
}
var funHike = {name,elevation,print}
funHike.print() // Mt. Tallac is 9738 feet tall
注意,这里我们是使用关键字
this
访问对象属性的。
当定义了对象方法后,就不需要再使用关键字
function
了(参见示例
2-5
)。
示例
2-5
:新旧对象语法比较
//
旧方案
var skier = {
name: name,
sound: sound,
powderYell: ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

React快速上手开发

React快速上手开发

Stoyan Stefanov
流畅的Python

流畅的Python

Luciano Ramalho
C++语言导学(原书第2版)

C++语言导学(原书第2版)

本贾尼 斯特劳斯特鲁普

Publisher Resources

ISBN: 9787519814236