首页 > 其他 > 详细

构造函数模式

时间:2016-12-29 11:05:23      阅读:91      评论:0      收藏:0      [点我收藏+]
    function student(props){
        this.name=props.name || 匿名;//默认是匿名
        this.grade=props.grade || 1;
    }
    student.prototype.hello=function(){
        console.log(hello +this.name);
    }
    function createStudent(props){
        return new student(props||{})
    }
    var xiaoming=createStudent({
        name:xiaoming
    });
    xiaoming.hello();//hello xiaoming

传进一个数组 

    function animal(name,age,grade){
        this.name=name;
        this.age=age;
        this.grade=grade;
    }
    animal.prototype.hello=function(){
        console.log(hello +this.name);
    }
    var chicken=new animal(chicken,3,12);
    chicken.hello();//hello chicken

我理解的构造函数就是用new()实例化去构造

构造函数模式

原文:http://www.cnblogs.com/lwwen/p/6231839.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!