Make a Person(freecodecamp高级算法7)

用下面给定的方法构造一个对象.

方法有 getFirstName(), getLastName(), getFullName(), setFirstName(first), setLastName(last), and setFullName(firstAndLast).

所有有参数的方法只接受一个字符串参数.

所有的方法只与实体对象交互.

var Person = function(firstAndLast) {
                    
                    this.setFullName = function(name){
                        firstAndLast = name;
                    }
                    
                    this.setFirstName = function(name){
                        firstAndLast = name + " " + this.getLastName();
                    }
                    
                    this.setLastName = function(name){
                        firstAndLast = this.getFirstName() + " " + name;
                    }
                    
                    this.getFullName = function(){
                        return firstAndLast;
                    }
                    
                    this.getFirstName = function(){
                        return firstAndLast.split(" ")[0];
                    }
                    
                    this.getLastName = function(){
                        return firstAndLast.split(" ")[1];
                    }
                    
                    
                    
                };
                
                var bob = new Person('Bob Ross');
                bob.setFullName("Haskell Curry");alert(bob.getFullName());

 

posted @ 2017-08-31 14:16  随风而逝XXX  Views(153)  Comments(0Edit  收藏  举报