【发布时间】:2016-12-08 21:07:33
【问题描述】:
有什么办法可以制作这段代码:
function person(first, last, age, eye) {
this.firstName = first;
this.lastName = last;
this.age = age;
this.eyeColor = eye;
}
看起来与此类似:
function person(first, last, age, eye) {
this = {
firstName: first,
lastName: last,
age: age,
eyeColor: eye
}
}
第一个方法要初始化很多变量,对于想要优化一切的程序员来说,这看起来很愚蠢。
【问题讨论】:
-
Object.assign(this,{ firstName: first, lastName: last, age: age, eyeColor: eye }) -
第一种方法在我看来并不愚蠢。它简单易读。
标签: javascript class object optimization