面向对象编程和简单的设计模式1

先说说,
设计模式:
直接,
简单,
暴力。
上代码:

//单体模式
var teacer={
    name:'lewis',
    age:33,
    showName:function(){
        return this.name
    }
}
teacer.showName();

//原型模式
//属性放在构造函数里,方法放在原型上
function teacer(name,age){
    this.name=name;
    this.age=age;
};
teacer.prototype.showName=function(){
    return this.name;
}
var lewis=new teacer('lewis',17);

lewis.showName();

//伪类模式
class teacer{
    constructor(name,age){
        this.name=name;
        this.age=age;
    }
    showName(){
        return this.name;
    }
}

var lewis=new teacer('lewis',22);
lewis.showName();

评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理