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. 基于vue项目的js工具方法汇总

    以下是个人过去一年在vue项目的开发过程中经常会用到的一些公共方法,在此进行汇总,方便以后及有需要的朋友查看~ let util = {}; /** * @description 日期格式化 * @p ...

  2. Windows10 下安装 Mongodb

    一.先登录Mongodb官网https://www.mongodb.com/download-center#community 下载   安装包.32.64位的都行.

  3. Pyhton学习——Day10

    #################################################################################################### ...

  4. video标签实现简单视频背景+遇到问题(视频无法显示,不能自动播放)

    最近看网上有一些网站首页背景是炫酷的视频背景,就想模拟一个 1.video标签简介 video标签定义视频,就是可以在网页上实现视频的播放,详情见http://www.w3school.com.cn/ ...

  5. CentOS 7 安装配置MySQL

    环境 CentOS Linux release 7.5.1804 (Core) MySQL:mysql80-community-release-el7-1 检查: 在centos7中默认的是maria ...

  6. HDU 1576 A/B( 逆元水 )

    链接:传送门 思路: 现在给出 n = A % 9973,n = A - A/9973×9973,已知 B|A ,设 A = Bx,可以得到如下形式的式子:Bx + 9973×y = n ,因为gcd ...

  7. LCA题集

    点的距离(模板题) 树中两点间的距离就是d[u] + d[v] - 2 * d[lca(u, v)] #include<bits/stdc++.h> #define REP(i, a, b ...

  8. 接口测试及接口Jmeter工具介绍

    一.接口类型及数据传递的格式 接口类型: 1.HTTP接口:通过GET或POST来获取数据,在数据处理上效率比较高 2.WebServer接口:通过SOAP协议来获取数据,比起http来说处理更加复杂 ...

  9. oracle 控制语句

    PL输出语句 set serverout on; -- 开启PL的输出语句功能declare n number:=1; -- 声明一个number型的变量n,并赋值为1 v varchar2(20): ...

  10. mysql int(m)与int(m)的差别

    预计大多数開始接触mysql的朋友们都会有这个问题:int(M) 里面的数值究竟是什么意思? 依据相关资料总结了下: int(M) zerofill,加上zerofill后M才表现出有点点效果,比方  ...