FCC(ES6写法) Make a Person

用下面给定的方法构造一个对象.

方法有 getFirstName(), getLastName(), getFullName(), setFirstName(first), setLastName(last), and setFullName(firstAndLast).

所有有参数的方法只接受一个字符串参数.

所有的方法只与实体对象交互.

思路:

考察构造函数,直接用ES6很简单。 

var Person = function(firstAndLast) {
  let first, last;
  this.getFirstName = () => first;
  this.getLastName = () => last;
  this.getFullName = () => first + ' ' + last;
  this.setFirstName = firstName => first = firstName;
  this.setLastName = lastName => last = lastName;
  this.setFullName = name => {
    name = name.split(' ');
    first = name[0];
    last = name[1];
  };
  this.setFullName(firstAndLast);
};
var bob = new Person('Bob Ross');
bob.getFullName();

  

如果有不明白的地方请留言,如果有更好更简便更优化的方法请留言,谢谢。

更多内容请访问我的个人博客: Bblog

猜你喜欢

转载自www.cnblogs.com/blackchaos/p/9144076.html
今日推荐