ES6 class

 

ES6比从前直接用function 做constructor 更接近JAVA,PHP之类的样子

class Cat {
  constructor(look,run){
    this.look = look;
    this.run = run ;
  }
}

let subie = new Cat("adorable","run like a cat")
let yuhki = new Cat("cute","run like a storm")
console.log(yuhki.run)
Cat.prototype.meow= function(){
  return 'am i '+ this.look +' ? '
}
console.log(yuhki.meow());

  

从前的 :

function Cat (look,run){
this.look = look;
this.run = run ;
}
var yuhki = new Cat("cute","run like a storm")//yuhki.constructor 指向 function Cat 
//在new创建对象的时候,会把function Cat 的 prototype属性指向的prototype对象的 construtor属性指向的constructor对象设置为function Cat。
//用new创造的对象和函数的区别,对象是没prototype属性的。但是有__proto__,指向该对象的constructor的prototype。 console.log(yuhki.run) Cat.prototype.meow= function(){ //一样,向Cat.prototype对象 中加入f() return 'am i '+ this.look +' ? ' } console.log(yuhki.meow());//call f() from prototype



posted @ 2017-11-08 17:18  Esther_Cheung  阅读(154)  评论(0)    收藏  举报