类及对象构建算法挑战
用方法 :
getFirstName(), getLastName(), getFullName(),
setFirstName(first), setLastName(last),
setFullName(firstAndLast)
构造一个对象.
所有有参数的方法只接受一个字符串参数.
所有的方法只与实体对象交互.
var Person = function(firstAndLast) {
this.getFullName = function () {
console.log("FullName: ",firstAndLast);
return firstAndLast;
}
this.getLastName = function () {
console.log("LastName: ",firstAndLast.split(" ")[1]);
return firstAndLast.split(" ")[1];
}
this.getFirstName = function () {
console.log("FirstName: ",firstAndLast.split(" ")[0]);
return firstAndLast.split(" ")[0];
}
this.setFullName = function (fullName) {
firstAndLast = fullName;
console.log("更新--FullName--后: ",firstAndLast);
}
this.setLastName = function (last) {
var nameTemp = [];
nameTemp = firstAndLast.split(" ");
nameTemp[1] = last;
firstAndLast = nameTemp.join(" ");
console.log("更新--LastName--后: ",firstAndLast);
}
this.setFirstName = function (first) {
var nameTemp = [];
nameTemp = firstAndLast.split(" ");
nameTemp[0] = first;
firstAndLast = nameTemp.join(" ");
console.log("更新--FirstName--后: ",firstAndLast);
}
};
var bob = new Person('Bob Ross');
bob.getFirstName();
bob.getLastName();
bob.getFullName();
console.log();
bob.setFirstName('Su');
bob.setFullName('Zan Mi');
bob.setLastName('San');
FirstName: Bob
LastName: Ross
FullName: Bob Ross
更新--FirstName--后: Su Ross
更新--FullName--后: Zan Mi
更新--LastName--后: Zan San