15.es6中的类

1.使用class定义一个类:类的声明类的表达式

//方法一:类的声明
class Person {

}
//方法二:类的表达式
const Student = class {

}

console.log(Person.prototype) //{}
console.log(Object.getOwnPropertyDescriptors(Person.prototype))
// {
//   constructor: {
//     value: [class Person],
//     writable: true,
//     enumerable: false,
//     configurable: true
//   }
// }
console.log(Person.prototype.__proto__)//[Object:null prototype] {}
console.log(Person.prototype.constructor) //[class Person]
console.log(typeof Person) //function

var p = new Person()
console.log(p.__proto__ === Person.prototype) //true

2.类的构造函数

当使用new 调用Person类创建一个对象时,传递的参数如果传递给类?

class Person {
}
var p = new Person('小名', 30)
var s = new Person('小红', 20)

通过new调用类传递的参数,会作为参数传递给类的构造函数

定义类的时候,
如果没有明确定义类的构造方法,会使用类的默认的构造方法
如果明确定义了构造方法,就使用定义的构造方法
一个类只有一个构造方法

// 定义类的时候,
// 如果没有明确定义类的构造方法,会使用类的默认的构造方法
// 如果明确定义了构造方法,就使用定义的构造方法
// 一个类只有一个构造方法

class Student {}

class Person {
  constructor(name, age) {
    this.name = name;
    this.age = age;
  }
}
console.log(Student.constructor); // [Function: Function] 输出类的默认构造方法
console.log(Person.constructor); // [Function: Function]

var p = new Person("小名", 30);
var s = new Person("小红", 20);
console.log(p.name, p.age); //小名 30
console.log(s.name, s.age); //小红 20

当使用new 调用类时,会执行类的构造函数,
调用类时传递的参数,会作为参数传给类的构造函数

class Person {
  //constructor为类的构造函数(构造方法)
  //类的构造函数被调用时:
  //1.在内存中创建一个新对象(空对象)var moni = {}
  //2.这个对象内部的[[prototype]]原型对象会指向类的prototype原型对象  moni.__proto__ = Person.prototype
  //3.this指向这个对象 this = moni
  //4.执行构造函数内部的代码
  //5.如果构造函数没有返回非空对象,则返回创建出来的新对象 return moni
  constructor(name, age) {
    this.name = name
    this.age = age
  }
}


//当使用new 调用类时,会执行类的构造函数,
//调用类时传递的参数,会作为参数传给类的构造函数
var p = new Person('小名', 30)
var s = new Person('小红', 20)

3.给类的prototype原型对象上添加方法

class Person {
  constructor(name, age) {
    //定义的属性定义在类的实例上
    this.name = name;
    this.age = age;
  }

  //在类的prototype原型对象上添加方法,不可枚举
  eatting() {
    console.log(this.name + " is eatting");
  }
}

var p = new Person("xiaoming", 30);
p.eatting(); //xiaoming is eatting

console.log(p); //Person { name: 'xiaoming', age: 30 }
console.log(Person.prototype); //{} 定义类时,添加到类的prototype原型对象上的方法是不可枚举的
console.log(Object.getOwnPropertyDescriptors(Person.prototype));
// {
//   constructor: {
//     value: [class Person],
//     writable: true,
//     enumerable: false,
//     configurable: true
//   },
//   eatting: {
//     value: [Function: eatting],
//     writable: true,
//     enumerable: false,
//     configurable: true
//   }
// }

相当于下面:

function Person(name, age) {
    this.name = name;
    this.age = age;
}
Person.prototype.eatting = function () {
  console.log(this.name + " is eatting");
}

4.给类的prototype原型对象上添加访问器方法

class Person {
  constructor(name, age) {
    //定义的属性定义在类的实例上
    this.name = name;
    this._age = age;
  }

  //在类的prototype原型对象上添加方法,不可枚举,被类的实例调用
  eatting() {
    console.log(this.name + " is eatting");
  }

  //在类的prototype原型对象上定义访问器方法,不可枚举,被类的实例调用
  get age() {
    return this.age;
  }
  set age(newVal) {
    this._age = newVal
  }


}

var p = new Person('xiaoming', 30)
p.eatting() //xiaoming is eatting

console.log(p) //Person { name: 'xiaoming', _age: 30 }
console.log(Person.prototype) // {}
console.log(Object.getOwnPropertyDescriptors(Person.prototype)) 
// {
//   constructor: {
//     value: [class Person],
//     writable: true,
//     enumerable: false,
//     configurable: true
//   },
//   eatting: {
//     value: [Function: eatting],
//     writable: true,
//     enumerable: false,
//     configurable: true
//   },
//   age: {
//     get: [Function: get age],
//     set: [Function: set age],
//     enumerable: false,
//     configurable: true
//   }
// }

5.class类中定义静态方法

class Person {
  constructor(name, age) {
    //定义的属性定义在类的实例上
    this.name = name;
    this.age = age;
  }

  //在类的prototype原型对象上添加方法,不可枚举,被类的实例调用
  eatting() {
    console.log(this.name + " is eatting");
  }

  //在类上添加静态方法,不可枚举,被类本身调用
  static createPerson() {
    return new Person("甲", 30);
  }
}

var p = new Person("xiaoming", 30);
p.eatting(); //xiaoming is eatting

console.log(p); //Person { name: 'xiaoming', age: 30 }
console.log(Object.getOwnPropertyDescriptors(Person.prototype));
// {
//   constructor: {
//     value: [class Person],
//     writable: true,
//     enumerable: false,
//     configurable: true
//   },
//   eatting: {
//     value: [Function: eatting],
//     writable: true,
//     enumerable: false,
//     configurable: true
//   },
// }
console.log(Person); //[class Person]
console.log(Object.getOwnPropertyDescriptors(Person));
// {
//   length: { value: 2, writable: false, enumerable: false, configurable: true },
//   prototype: {
//     value: {},
//     writable: false,
//     enumerable: false,
//     configurable: false
//   },
//   createPerson: {
//     value: [Function: createPerson],
//     writable: true,
//     enumerable: false,
//     configurable: true
//   },
//   name: {
//     value: 'Person',
//     writable: false,
//     enumerable: false,
//     configurable: true
//   }
// }
console.log(Person.createPerson()); //Person { name: '甲', age: 30 }

相当于

function Person (name, age){
  this.name = name;
  this.age = age;
}
Person.prototype.eatting = function () {
    console.log(this.name + " is eatting");
  }
Person.createPerson = function () {
  return new Person("甲", 30);
}

6.class类中的继承

子类如果实现了继承,在子类构造函数中访问this或return之前必须通过super调用父类构造函数

class Person {
  constructor(name, age) {
    this.name = name;
    this.age = age;
  }
}

//子类如果实现了继承,在子类构造函数中访问this或return之前必须通过super调用父类构造函数
class Student extends Person {
  constructor(name, age, address) {
    super(name, age);
    this.address = address;
  }
}

var stu = new Student('小名', 30, '北京') 
console.log(stu) //Student { name: '小名', age: 30, address: '北京' }
console.log(stu.__proto__)//Person {} 输出结果为一个对象,属于Person类
//因为stu.__proto__为Student.prototype,
//而Student.prototype为一个对象,此对象的__proto__原型对象为Person.prototype 
//所以Student.prototype为Person类的一个实例对象,即stu.__proto__为一个Person类的一个实例对象
//stu的[[prototype]]原型对象为Person的实例,为[[prototype]]原型对象为Person.prototype原型对象的对象
console.log(stu.__proto__.__proto__ === Person.prototype) //true

子类继承父类的原型对象上的方法和子类继承父类上的静态属性和方法

class Person {
  constructor(name, age) {
    this.name = name;
    this.age = age;
  }

  running() {
    console.log(this.name + " is running");
  }
  static createPerson() {
    console.log("创建person");
  }
}

//子类如果实现了继承,在子类构造函数中访问this或return之前必须通过super调用父类构造函数
class Student extends Person {
  constructor(name, age, address) {
    super(name, age);
    this.address = address;
  }
  //在子类的原型对象上定义running方法
  eatting() {
    console.log(this.name + " is eatting");
  }

  //在子类上定义静态方法
  static createStudent() {
    console.log("创建student");
  }
}

var stu = new Student("小名", 30, "北京");
console.log(stu); //Student { name: '小名', age: 30, address: '北京' }
stu.running(); //小名 is running
stu.eatting(); //小名 is eatting
Student.createStudent(); //创建student
Student.createPerson(); //创建person

console.log(stu.__proto__); //Person {} 为一个Person类的对象,即类Person的一个实例
console.log(Object.getOwnPropertyDescriptors(stu.__proto__));
// {
//   constructor: {
//     value: [class Student extends Person],
//     writable: true,
//     enumerable: false,
//     configurable: true
//   },
//   eatting: {
//     value: [Function: eatting],
//     writable: true,
//     enumerable: false,
//     configurable: true
//   }
// }
//定义子类时,定义在子类prototype原型对象的方法,在子类的prototype对象上

console.log(stu.__proto__.__proto__); //{} 为父类Person类的prototype原型对象
console.log(stu.__proto__.__proto__ === Person.prototype); //true
console.log(Object.getOwnPropertyDescriptors(stu.__proto__.__proto__));
// {
//   constructor: {
//     value: [class Person],
//     writable: true,
//     enumerable: false,
//     configurable: true
//   },
//   running: {
//     value: [Function: running],
//     writable: true,
//     enumerable: false,
//     configurable: true
//   }
// }

// 定义子类时,继承的父类的prototype原型对象上的方法,依然在父类的prototype原型对象上



console.log(Student) //[class Student extends Person]
console.log(Object.getOwnPropertyDescriptors(Student))
// {
//   length: { value: 3, writable: false, enumerable: false, configurable: true },
//   prototype: {
//     value: Person {},
//     writable: false,
//     enumerable: false,
//     configurable: false
//   },
//   createStudent: {
//     value: [Function: createStudent],
//     writable: true,
//     enumerable: false,
//     configurable: true
//   },
//   name: {
//     value: 'Student',
//     writable: false,
//     enumerable: false,
//     configurable: true
//   }
// }
console.log(Student.__proto__ === Person) //true 
// 因为子类的[[prototype]]原型对象为父类Person,所以可以通过子类Student调用父类Person上的方法

类对父类方法的重写
在子类定义prototype原型对象方法内部,可以通过super调用父类的prototype对象,父类prototype对象上的方法内部的this被绑定为子类prototype原型对象方法内部的this
在子类定义静态方法内部,可以通过super调用父类

class Person {
  constructor(name, age) {
    this.name = name;
    this.age = age;
  }

  running() {
    console.log(this.name + ' is running')
  }
  eatting() {
    console.log(this.name + ' is eatting')
  }
  static createPerson() {
    console.log('第一步')
  }
}

//子类如果实现了继承,在子类构造函数中访问this或return之前必须通过super调用父类构造函数
class Student extends Person {
  constructor(name, age, address) {
    super(name, age);
    this.address = address;
  }

  //子类对父类方法的重写
  eatting() {
    console.log('今天吃的很饱')
  }
  //在子类的原型对象上定义running方法
  running() {
    //通过super调用父类的原型对象,从父类的原型对象上获取running方法,
    //此方法执行时。方法内容的this被绑定为当前作用域的this
    super.running()
    console.log(this.name + ' run fastest')
  }

  //在子类上定义静态方法
  static createPerson() {
    //通过super获取父类,调用父类上的静态方法
    super.createPerson()
    console.log('第二步')
  }
}

var stu = new Student('小名', 30, '北京') 
console.log(stu) //Student { name: '小名', age: 30, address: '北京' }
stu.running() 
//小名 is running
//小名 run fastest

Student.createPerson()
// 第一步
// 第二步
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 216,470评论 6 501
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,393评论 3 392
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 162,577评论 0 353
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,176评论 1 292
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,189评论 6 388
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,155评论 1 299
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,041评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,903评论 0 274
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,319评论 1 310
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,539评论 2 332
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,703评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,417评论 5 343
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,013评论 3 325
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,664评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,818评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,711评论 2 368
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,601评论 2 353

推荐阅读更多精彩内容

  • js常用的编程模式有面向过程编程和面向对象编程(1)面向过程编程“面向过程”(Procedure Oriented...
    xiexw阅读 199评论 0 1
  • 文章首发于 个人博客 目录 class 静态方法 静态属性 继承 super class class是一个语法糖,...
    IOneStar阅读 1,650评论 0 1
  • 前言 先来复习一下原型对象,原型对象就相当于一个公共的区域,所有同一个类的实例都可以访问到这个原型对象。当我们访问...
    独善其身_1558阅读 216评论 0 0
  • 简介 类的由来 JavaScript 语言中,生成实例对象的传统方法是通过构造函数。下面是一个例子。 上面这种写法...
    IT杨阅读 791评论 0 2
  • ES6中定义类.继承以及对象的操作0712 1.定义类的不同 回顾一下以前学习的在ES6之前定义一个类(不明显,和...
    煤球快到碗里来阅读 438评论 0 0