美文网首页
es5 es6静态方法、类、单例模式

es5 es6静态方法、类、单例模式

作者: xinhui9056 | 来源:发表于2018-09-18 10:23 被阅读0次
es5中的类和静态方法
function Person(name,age) {
//构造函数里面的方法和属性
      this.name=name;
      this.age=age;
      this.run=function(){
           console.log(`${this.name}---${this.age}`)
       }
}
//原型链上面的属性和方法可以被多个实例共享
Person.prototype.sex='男';
Person.prototype.work=function(){
      console.log(`${this.name}---${this.age}---${this.sex}`);
 }
//静态方法
Person.setName=function(){
      console.log('静态方法');
}
var p=new Person('zhangsan','20');   /*实例方法是通过实例化来调用的,静态是通过类名直接调用*/
p.run();
p.work();

Person.setName();  /*执行静态方法*/
es5继承
/*
原型链继承和对象冒充继承
对象冒充继承:没法继承原型链上面的属性和方法
原型链继承:可以继承构造函数里面以及原型链上面的属性和方法,实例化子类的时候没法给父类传参
* */
function  Person(name,age) {
    this.name=name;
    this.age=age;
    this.run=function(){
        console.log(this.name+'---'+this.age);
    }
}
Person.prototype.work=function(){
    console.log('work');
}

function Web(name,age){
    Person.call(this,name,age);  /*对象冒充实现继承*/
}

Web.prototype=new Person();
var w=new Web('李四',20);
w.run();
w.work();  //w.work is not a function
es6中的类
//定义Person类
class Person{
    constructor(name,age) {   /*类的构造函数,实例化的时候执行,new的时候执行*/
        this._name=name;
        this._age=age;
    }
    getName(){
        console.log(this._name);
    }
    setName(name){
        this._name=name
    }
}
var p=new Person('张三1','20');
p.getName();
p.setName('李四');
p.getName();
es6里面的继承
class Person{
   constructor(name,age){
       this.name = name;
       this.age =age;
   }
   getInfo(){
       console.log(`姓名:${this.name} 年龄:${this.age}`)
   }
   run(){
       console.log('run')
   }
}

class Web extends Person{
   constructor(name,age,sex){
    super(name,age);
    this.sex = sex;
   }
   print(){
    console.log(this.sex);
   }
}

var w = new Web('Li',38,'男');
w.getInfo()
w.print()
es6里面的静态方法
class Person{

    constructor(name){

        this._name=name;  /*属性*/
    }
    run(){  /*实例方法*/

        console.log(this._name);
    }
    static work(){   /*静态方法*/
        console.log('这是es6里面的静态方法');
    }
}
Person.instance='这是一个静态方法的属性';


var p=new  Person('张三');

p.run();
Person.work();   /*es6里面的静态方法*/

console.log(Person.instance);
es6单例模式
class Db {
    static getInstance(){   /*单例*/
        if(!Db.instance){
            Db.instance=new Db();
        }
        return Db.instance;
    }
    constructor(){
        console.log('实例化会触发构造函数');
        this.connect();
    }
    connect(){
        console.log('连接数据库');
    }
    find(){
        console.log('查询数据库');
    }
}
var myDb=Db.getInstance();
var myDb2=Db.getInstance();
var myDb3=Db.getInstance();
var myDb4=Db.getInstance();
myDb3.find();
myDb4.find();

转载:https://blog.csdn.net/grace_fang/article/details/80482326

相关文章

  • es5 es6静态方法、类、单例模式

    es5中的类和静态方法 es5继承 es6中的类 es6里面的继承 es6里面的静态方法 es6单例模式 转载:h...

  • 静态方法、单例模式区别

    观点一:(单例) 单例模式比静态方法有很多优势:首先,单例可以继承类,实现接口,而静态类不能(可以集成类,但不能集...

  • class类的使用

    通过class关键字可以定义类,可以实现单例模式,访问器属性,静态方法,extends继承 普通写法 es6 cl...

  • 02-创建型模式-单例模式

    单例模式 单例模式:类只能创建一个实例,并提供对实例的静态访问方法。要点:私有构造器静态私有变量声明可全局访问的公...

  • 技术文章收集

    单例模式Java 类加载静态类加载时机

  • 设计模式小结

    单例模式 单例模式主要包括懒汉式、饿汉式、双重检查锁、静态内部类、枚举类。注意的点:构造方法为private, 变...

  • 深入es6之class

    es5定义一个类 es6定义一个类 es6原型方法(内部this是实例化的类) 静态方法(内部this是类) 继承

  • 单例模式

    单例模式 单例模式的特点: 构造函数不对外开放,一般为Private 通过一个静态方法返回一个单例类对象 确保单例...

  • 设计模式系列-单例模式

    设计模式系列 - 单例模式 是什么? 定义: 确保一个类只有一个实例,并且提供访问该实例的静态方法。 单例的特点:...

  • Kotlin单例模式使用案例

    Kotlin单例模式 1、kotlin的object就是一个单例模式,所有字段都是static静态,方法不是静态2...

网友评论

      本文标题:es5 es6静态方法、类、单例模式

      本文链接:https://www.haomeiwen.com/subject/ymyynftx.html