
28
|
第
2
章
getFakeMembers(5).then(
members => console.log(members),
err => console.error(
new Error("cannot load members from randomuser.me"))
)
Promise
对象处理异步请求的方法更容易,效果也更好,因为我们在
JavaScript
中必须
处理大量的异步数据。读者还会发现在
Node.js
中
Promise
对象的身影随处可见,因此
深刻理解
Promise
对象对于当前的
JavaScript
工程师来说至关重要。
类
在以前的
JavaScript
中,官方规范中并没有类的概念。类型定义是通过函数完成的。
我们创建了一个函数,然后通过原型(
prototype
)在函数对象上定义方法:
function Vacation(destination, length) {
this.destination = destination
this.length = length
}
Vacation.prototype.print = function() {
console.log(this.destination + " | " + this.length + " days")
}
var maui = new Vacation("Maui", 7);
maui.print(); // Maui | 7
如果读者以前使用的是经典的面向对象程序设计方法进行开发的,那么这一点可能会 ...