码迷,mamicode.com
首页 > 其他好文 > 详细

FCC(ES6写法) Make a Person

时间:2018-06-06 12:32:58      阅读:119      评论:0      收藏:0      [点我收藏+]

标签:博客   http   tar   person   target   写法   stand   blog   ros   

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

方法有 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

FCC(ES6写法) Make a Person

标签:博客   http   tar   person   target   写法   stand   blog   ros   

原文地址:https://www.cnblogs.com/blackchaos/p/9144076.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!