Make a Person

Fill in the object constructor with the following methods below:

getfirstname()
getLastName()
getFullName()
setFirstName(first)
setLastName(last)
setFullName(firstAndLast)

Run the tests to see the expected output for each method.

The methods that take an argument must accept only one argument and it has to be a string.

These methods must be the only available means of interacting with the object.

考察闭包

直接上代码好了

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

这里要注意的是this的使用。上述代码中,只有6个方法使用了this

firstName, lastName不使用this是为了防止Person的实例直接访问。

要真正实现私有属性,一种方式是使用闭包。即使这样做会增加资源占用。

要想访问或设置firstName, lastName,必须使用给定的方法。

下面是运行结果图。

FCC高级编程篇之Make a Person的更多相关文章

  1. FCC高级编程篇之Validate US Telephone Numbers

    Validate US Telephone Numbers Return true if the passed string is a valid US phone number. The user ...

  2. FCC高级编程篇之Exact Change

    Exact Change Design a cash register drawer function checkCashRegister() that accepts purchase price ...

  3. FCC高级编程篇之Symmetric Difference

    Symmetric Difference Create a function that takes two or more arrays and returns an array of the sym ...

  4. FCC高级编程篇之Record Collection

    Record Collection You are given a JSON object representing a part of your musical album collection. ...

  5. (十三) [终篇] 一起学 Unix 环境高级编程 (APUE) 之 网络 IPC:套接字

    . . . . . 目录 (一) 一起学 Unix 环境高级编程 (APUE) 之 标准IO (二) 一起学 Unix 环境高级编程 (APUE) 之 文件 IO (三) 一起学 Unix 环境高级编 ...

  6. unix环境高级编程基础知识之第二篇(3)

    看了unix环境高级编程第三章,把代码也都自己敲了一遍,另主要讲解了一些IO函数,read/write/fseek/fcntl:这里主要是c函数,比较容易,看多了就熟悉了.对fcntl函数讲解比较到位 ...

  7. C++面向对象高级编程(四)基础篇

    技术在于交流.沟通,转载请注明出处并保持作品的完整性. 一.Static 二.模板类和模板函数 三.namespace 一.Static 静态成员是“类级别”的,也就是它和类的地位等同,而普通成员是“ ...

  8. C++面向对象高级编程(三)基础篇

    技术在于交流.沟通,转载请注明出处并保持作品的完整性. 概要 一.拷贝构造 二.拷贝赋值 三.重写操作符 四.生命周期 本节主要介绍 Big Three 即析构函数,拷贝构造函数,赋值拷贝函数,前面主 ...

  9. C++面向对象高级编程(二)基础篇

    技术在于交流.沟通,转载请注明出处并保持作品的完整性. 概要 知识点1.重载成员函数 知识点2 . return by value, return by reference 知识点3 重载非成员函数 ...

随机推荐

  1. quartz定时任务框架调度机制解析

    转自集群调度机制调研及源码分析 quartz2.2.1集群调度机制调研及源码分析引言quartz集群架构调度器实例化调度过程触发器的获取触发trigger:Job执行过程:总结:附: 引言 qurat ...

  2. 2015 Objective-C 新特性

    Overview 自 WWDC 2015 推出和开源 Swift 2.0 后,大家对 Swift 的热情又一次高涨起来,在羡慕创业公司的朋友们大谈 Swift 新特性的同时,也有很多像我一样工作上依然 ...

  3. ajax第二天学习

    post方式发送请求 要首先设置请求头(参数设置为ajax.setRequestHeader("content-type","application/x-www-form ...

  4. kvm三个kernel相关的调优

    今天在杭州参加淘宝嘉年华技术沙龙,主题是虚拟化和云计算,三个讲演: 淘宝网子团分享淘宝kvm技术的使用 华为的杨晓伟介绍虚拟化技术 阿里云郑永升介绍弹性云计算技术 其中KVM 调优,三点值得关注的: ...

  5. luogu 自适应Simpson1

    自适应simpson1 题意 求一个定积分 (可以手推公式,但是我不想推怎么办) 解法 用一个又一个的二次函数覆盖原函数,则可以近似的得到原函数的积分.(这就是Simpson) 模板在下面: #inc ...

  6. 代理上网环境配置docker私有库

    最后更新时间:2018年12月27日 Docker使用代理上网去 pull 各类 images,需要做如下配置: 创建目录: /etc/systemd/system/docker.service.d ...

  7. 解决tpcc_load 报错 error while loading shared libraries: libmysqlclient.so.20

    在刚开始导入tpcc数据仓库时,可能会遇到 error while loading shared libraries: libmysqlclient.so.20这个错误,找不到库文件. 但是,通过fi ...

  8. 利用Python对文件进行批量重命名

    最近几天工作的内容是对40个项目进行考核,每个项目都需要一个考核评分表,已经有了项目的列表. 如果用常规的方法,需要复制40个文件,并逐个修改,不光工作量大,也容易出错,后期修改也不方便. 于是想到了 ...

  9. HDU 4335 Contest 4

    利用降幂公式..呃,还是自己去搜题解吧.知道降幂公式后,就不难了. #include <iostream> #include <cstdio> #include <alg ...

  10. [ReactVR] Add Lighting Using Light Components in React VR

    In order to illuminate a scene containing 3D objects a lighting setup is required. In this lesson we ...