类及对象构建算法挑战
问题:
用下面给定的方法构造一个对象.
方法有 getFirstName(), getLastName(), getFullName(), setFirstName(first), setLastName(last), and setFullName(firstAndLast).
所有有参数的方法只接受一个字符串参数.
所有的方法只与实体对象交互.
要求:
无
问题答案:
var Person = function(firstAndLast) {
var arr = firstAndLast.split(' '),
first = arr[0],
last = arr[1],
fullName = firstAndLast;
this.getFirstName = function(){
return first;
};
this.getLastName = function(){
return last;
};
this.getFullName = function(){
return fullName;
};
this.setFirstName = function(f){
first = f;
fullName = first + ' ' + last;
};
this.setLastName = function(l){
last = l;
fullName = first + ' ' + last;
};
this.setFullName = function(fn){
arr = fn.split(' ');
fullName = fn;
first = arr[0];
last = arr[1];
};
};
var bob = new Person('Bob Ross');
bob.getFullName();