ES6: Class类

2016-09-09  本文已影响83人  打不过就加入他

Es6提供了新的构造函数写法,引入了Class这个关键字来声明一个构造函数

function Person(x,y){
  this.x = x;
  this.y = y;
}
Person.prototype.init = function(){}
//实例
new Person(1,2)
class Person(){
  constructor(x,y){
  this.x = x;
  this.y = y;
  }
  init(){}
}
//实例
new Person(1,2)

注意方法间不要加“,”

class List(){
  _get(){
  }
  init(){}
}
//_get方法我用"_"做标示,证明他只在内部使用,是私有方法
class Person(){
  constructor(x,y){
  }
}
class students extends Person(){
  constructor(x,y,z){
    super(x,y);
    this.z = z;
}
}

看到这里,其实是可以实现一个很简单的双向绑定

class bind(){
  constructor(ele){
    this.ele = document.getElementBy(ele)
  }
  get html() { 
    return this.element.innerHTML;
   } 
  set html(value) 
  { 
    this.element.innerHTML = value; 
  }
}
class Person(){
  static age(){ return 'hh'}
}
Person.age()  ==> hh
上一篇 下一篇

猜你喜欢

热点阅读