FCC高级编程篇之Make a Person
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的更多相关文章
- FCC高级编程篇之Validate US Telephone Numbers
Validate US Telephone Numbers Return true if the passed string is a valid US phone number. The user ...
- FCC高级编程篇之Exact Change
Exact Change Design a cash register drawer function checkCashRegister() that accepts purchase price ...
- FCC高级编程篇之Symmetric Difference
Symmetric Difference Create a function that takes two or more arrays and returns an array of the sym ...
- FCC高级编程篇之Record Collection
Record Collection You are given a JSON object representing a part of your musical album collection. ...
- (十三) [终篇] 一起学 Unix 环境高级编程 (APUE) 之 网络 IPC:套接字
. . . . . 目录 (一) 一起学 Unix 环境高级编程 (APUE) 之 标准IO (二) 一起学 Unix 环境高级编程 (APUE) 之 文件 IO (三) 一起学 Unix 环境高级编 ...
- unix环境高级编程基础知识之第二篇(3)
看了unix环境高级编程第三章,把代码也都自己敲了一遍,另主要讲解了一些IO函数,read/write/fseek/fcntl:这里主要是c函数,比较容易,看多了就熟悉了.对fcntl函数讲解比较到位 ...
- C++面向对象高级编程(四)基础篇
技术在于交流.沟通,转载请注明出处并保持作品的完整性. 一.Static 二.模板类和模板函数 三.namespace 一.Static 静态成员是“类级别”的,也就是它和类的地位等同,而普通成员是“ ...
- C++面向对象高级编程(三)基础篇
技术在于交流.沟通,转载请注明出处并保持作品的完整性. 概要 一.拷贝构造 二.拷贝赋值 三.重写操作符 四.生命周期 本节主要介绍 Big Three 即析构函数,拷贝构造函数,赋值拷贝函数,前面主 ...
- C++面向对象高级编程(二)基础篇
技术在于交流.沟通,转载请注明出处并保持作品的完整性. 概要 知识点1.重载成员函数 知识点2 . return by value, return by reference 知识点3 重载非成员函数 ...
随机推荐
- 自动化框架的两种断言设计(pytest 版)
自动化测试断言失败时,根据不同业务场景,可能需要立即终止或继续执行.这里以 Appium + pytest 为例. 一. 断言失败立即终止 用途一:用例的预期结果是其他用例的前提条件时,assert ...
- 一个关于传参数js数组的封装方法(寄生模式)
function createArr(){ var arr = new Array(); arr.push.apply(arr,arguments); arr.toJoin = function(){ ...
- poj 2828 Buy Tickets【线段树 单点更新】
倒着插,先不理解意思,后来看一篇题解说模拟一下 手动模拟一下就好理解了----- 不过话说一直写挫---一直改啊----- 好心塞------ #include <cstdio> #inc ...
- Python——Day4(基础知识练习二)
# 1.请用代码实现:利用下划线将列表的每一个元素拼接成字符串.# li = ['alex','eric','rain']# li = ['alex','eric','rain']# li2 = &q ...
- SASS概览
1.安装: sass需要使用ruby,首先安装ruby,之后: gem install sass 编译: sass input.scss output.css 2.快速入门: 变量: .scss 变量 ...
- 模板 FFT 快速傅里叶变换
FFT模板,原理不难,优质讲解很多,但证明很难看太不懂 这模板题在bzoj竟然是土豪题,服了 #include <cmath> #include <cstdio> #inclu ...
- python--(常用模块-3-正则表达式)
python--(常用模块-3-正则表达式) 正则表达式是对字符串操作的⼀种逻辑公式. 我们⼀般使⽤正则表达式对字符串进⾏匹 配和过滤. 使⽤正则的优缺点: 优点: 灵活, 功能性强, 逻辑性强. 缺 ...
- 前端的标配:npm是什么及其安装(含cnpm)
前端的标配:npm是什么及其安装 一:npm是什么及其来源 参考来源:npm是干什么的 总结:不需要去相关的网站下载依赖,用一个工具把这些依赖集中起来管理 NPM 的思路大概是这样的: 1)买个服务器 ...
- onkeydown、onkeypress、onkeyup、onblur、onchange、oninput、onpropertychange的区别
onkeydown:按下任何键(字母.数字.系统.tab等)都能触发,且对于字母不区分大小写: onkeypress:按下字母.数字键时触发,且对于字母区分大小写; onkeyup:相应的键和onke ...
- java类的属性
类的嵌套!!!!!!!!!! 首先我们创建一个学生卡卡号的一个类,这个类有两个属性,校园卡号和银行卡号 package cuteSnow; public class StudentCard { pub ...