使用工厂模式创建对象

对象:使用工厂方法创建对象:
function creatPerson(name,age,gender) {
        var obj=new Object();
        obj.name=name;
        obj.age=age;
        obj.gender=gender;
        obj.sayName=function () {
            console.log(this.name);
        }
        return obj;
    }
    var obj1=creatPerson("小猪",18,"man");
    obj1.sayName();
使用的构造函数都是Object这个类型。
导致我们无法区分。

猜你喜欢

转载自blog.csdn.net/weixin_44426449/article/details/107774499