类及对象构建算法挑战

类及对象构建算法挑战

用方法 :
	getFirstName(), getLastName(), getFullName(), 
	setFirstName(first), setLastName(last),  
	setFullName(firstAndLast)
构造一个对象.
所有有参数的方法只接受一个字符串参数.
所有的方法只与实体对象交互.
var Person = function(firstAndLast) {
    //  GET----------------------------------------
    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];
    }
    // SET----------------------------------------
    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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值