建造者模式

    var Human=function(param){
        this.skill=param && param.skill || '保密';
        this.hobby=param && param.hobby || '保密';
    }

    Human.prototype={
        getskill:function(){
            return this.skill;
        },
        getHobby:function(){
            return this.hobby;
        }
    }

    var Named=function(name){
        var that=this

        (function(name,that){
            that.wholeName=name;
            if(name.indexof(' ')>-1){
                that.FirstName=name.slice(0,name.indexof(' '))
                that.secondName=name.slice(name.indexof(' '))
            }
        })(name,that)
    }

    var Work=function(work){
        var that=this;

        (function(work,that){
            switch(work){
                case 'code':
                    that.work='工程师'
                    that.workDescript='每天沉醉于编程'
                    break;
                case 'UI':
                    that.work='平面设计师';
                    that.workDescript='每天沉醉于设计';
                    break; 
                case 'UE':
                    that.work='设计师';
                    that.workDescript='设计更似一种艺术';
                    break;
                case 'teach':
                    that.work='教师'
                    that.workDescript='分享也是一种快乐';
                    break;
                default:
                    that.work=work;
                    that.workDescript='对不起,我们还不清楚您所选择职位的相关描述'
            }
        })(work,that)
    }

    Work.prototype.changeWork=function(work){
        this.work=work;
    }

    Work.prototype.changeDescript=function(setence){
        this.workDescript=setence;
    }

    var Person=function(name,work){
        var _person=new Human();
        _person.name=new Named(name);
        _person.work=new Work(work);
        
        return _person;
    }

    var person=new Person('xiao ming','code');

    console.log(person.skill);
    console.log(person.name.FirstName)
    console.log(person.work.work)
    console.log(person.work.workDescript)
    person.work.changeDescript('更改职位描述')
    console.log(person.work.workDescript)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值