用javascript来模拟实现函数重载

//模拟函数重载
function person() {
    var xx=arguments;
    if(typeof xx[0]=='object'&&xx[0]){
        if(xx[0].name){
            this.name=xx[0].name;
        }
        if(xx[0].age){
            this.age=xx[0].age;
        }
    }else{
        if(xx[0]){
            this.name=xx[0];
        }
        if(xx[1]){
            this.age=xx[1];
        }
    }
}
person.prototype.toString=function () {
    return 'name='+this.name+',age='+this.age;
}
var std1=new person('lihua',27);
console.log(std1.toString());
var std2=new person({name:'liwei',age:24});
console.log(std2.toString())

猜你喜欢

转载自blog.csdn.net/lgl_19910331/article/details/81112439