面向对象写法模板

//构造函数
    function createPerson(name,age,work){ 
        this.name=name;
        this.age=age;
        this.work=work;
        //初始化
        this.inint();
    }
    //原型方法
    createPerson.prototype={
        //修正constructor
        constructor:createPerson,
        inint:function(){ 
            this.showMsg();
            this.sayHi();
        },
        showMsg:function(){ 
            //alert('我的名字是'+this.name+',年龄'+this.age+','+this.work);
        },
        sayHi:function(){ 
            //alert('hi!我是一个:'+this.work);
        }
    }
    //调用
    var p1=new createPerson('tom','21','学生');
    var p2=new createPerson('ollie','27','web前端工程师');
原文地址:https://www.cnblogs.com/ollie-sk8/p/4150682.html