美文网首页
es6中的类和es5的类

es6中的类和es5的类

作者: 2分_08b6 | 来源:发表于2019-03-05 17:07 被阅读0次

典型的es6类:
类的成员: 构造方法,属性,方法,静态方法等

class Parent {
  height = 20;
  constructor(name) {
    this.name = name;
  }

  static sayHello() {
    console.log('hello');
  }

  sayName() {
    console.log('my name is ' + this.name);
    return this.name;
  }
}
class Child extends Parent {
  constructor(name, age) {
    super(name);
    this.age = age;
  }

  sayAge() {
    console.log('my age is ' + this.age);
    return this.age;
  }
}

类的使用时new出来的,跟es5不一样; 类的写法跟es5也是不一样,那么有如下问题:

  1. es6这样的写法对应于es5写法,类定义哪些属性和方法是对应起来的
  2. 当继承发生的时候,过程是怎样的

1. es6这样的写法对应于es5写法,类定义哪些属性和方法是对应起来的

es5类的定义

function ClassName () {
  this.x = 1;
  this.y = 2;
  this.sayHello = function () {
    console.log('hello');
  }
}
ClassName.prototype.sayHello = function () {
  console.log('hello');
}
var className = new ClassName();
className.sayHello();

es6类的定义

class ClassName {
  x = 1;
  y = 2;
  constructor () {}
  sayHello () {
    console.log('hello');
  }
}
es5对象:

定义在实例对象上的:
this.x
this.y
this.sayHello
定义在类的原型上的:
ClassName.prototype.sayHello


es6对象:

定义在实例对象上的:
x = 1;
y = 2;
定义在原型上的:
sayHello方法(非静态方法)



另外,es6对象上的静态方法(前缀是static),不在原型上, 不可被实例化对象调用,且不能被子类的实例化对象调用,但是可以被子类继承,通过子类名调用.

2. 当继承发生的时候,过程是怎么样的

es5的继承

function Parent () {
  this.x = 1;
  this.y = 2;
}

function Child () {
  this.z = 3;
}

Child.prototype = new Parent();

var parent = new Parent();
var child = new Child();

es6的继承

class Parent {
  height = 20;
  static width = 30;
  constructor(name) {
    this.name = name;
  }

  static sayHello() {
    console.log('hello');
  }

  sayName() {
    console.log('my name is ' + this.name);
    return this.name;
  }
}

class Child extends Parent {
  constructor(name, age) {
    super(name);
    this.age = age;
  }

  sayAge() {
    console.log('my age is ' + this.age);
    return this.age;
  }
}

let parent = new Parent('Parent');
let child = new Child('Child', 18);

下面将探索继承发生了什么
es5的继承中,只存在单个原型链;
es6的继承中,同时存在两条原型链;

es5的继承如下:
在构造函数这条原型链上是不存在的;
在实例上的原型链是存在的,

function Parent () {
  this.x = 1;
  this.y = 2;
}

function Child () {
  this.z = 3;
}

Child.prototype = new Parent();

var parent = new Parent();
var child = new Child();

// 在构造函数这条原型链上是不存在的
console.log('Child.__proto__ === Parent', Child.__proto__ === Parent);

/**
在实例上的原型链存在
*/ 
// 子类实例继承自子类原型
console.log('Child.__proto__ === Child.prototype', child.__proto__ === Child.prototype);
// 子类原型继承自父类原型
console.log('Child.prototype.__proto__ === parent.prototype', Child.prototype.__proto__ === Parent.prototype);
// 父类实例继承自父类原型
console.log('parent.__proto__ === Parent.prototype', parent.__proto__ === Parent.prototype);
// 子类实例继承自父类实例(Child.prototype = new Parent();)
console.log('Child.prototype.__proto__ === parent.__proto__', Child.prototype.__proto__ === parent.__proto__);

es6的继承,存在2条原型链

class Parent {
  height = 20;
  static width = 30;
  constructor(name) {
    this.name = name;
  }

  static sayHello() {
    console.log('hello');
  }

  sayName() {
    console.log('my name is ' + this.name);
    return this.name;
  }
}

class Child extends Parent {
  constructor(name, age) {
    super(name);
    this.age = age;
  }

  sayAge() {
    console.log('my age is ' + this.age);
    return this.age;
  }
}

let parent = new Parent('Parent');
let child = new Child('Child', 18);
// 构造函数原型链
console.log('Child.__proto__ === Parent', Child.__proto__ === Parent);
console.log('Parent.__proto__ === Function.prototype', Parent.__proto__ === Function.prototype);
console.log('Function.prototype.__proto__ === Object.prototype', Function.prototype.__proto__ === Object.prototype);
console.log('Parent.__proto__ === Function.prototype', Object.prototype.__proto__ === null);

// 实例原型链
console.log('child.__proto__ === Child.prototype', child.__proto__ === Child.prototype);
console.log('Array.prototype.__proto__ === Object.prototype', Child.prototype.__proto__ === Parent.prototype);
console.log('Array.prototype.__proto__ === Object.prototype', Parent.prototype.__proto__ === Object.prototype);
console.log('Array.prototype.__proto__ === Object.prototype', Object.prototype.__proto__ === null);

最后附上一张图:


751551798315_.pic_hd.jpg

相关文章

  • day06-前端面试技巧-(面向对象)

    类与实例: 类的声明es5: es6中class的声明 生成实例实例类的对象(es5和es6的实例化方式是一样的)...

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

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

  • JS中的类

    如何定义一个类 ES5中定义一个类: ES6以后的语法(可以看做是ES5的语法糖): ES6的类,完全可以看作构造...

  • es6类

    es6类中对应es5的实现 alias = function|obj

  • Day2. JSX核心语法一, 跟着Demo入门JSX

    一. Javascript类的定义 JS语法补充 ES5中定义类 class ES6开始才有的关键字 ES6中通过...

  • ES6中的类

    昨天小编更新了一下关于es5中的类的处理,也顺便将es5中类的知识和语法简单回顾一下,今天小编将es6中的与类相关...

  • 九: ES6 Class 类

    前言 该部分为书籍 深入理解ES6 第九章(JS的类)笔记 ES5 中的仿类结构 在 ES5 中与类最接近的是: ...

  • ES6基本的语法(八) Class

    ES6 Class 和 ES5 的对比和基本说明 上面的代码中定义了一个“类” 类名是 Point。constru...

  • 简单总结ES6中的类定义语法和ES7的async/await

    学习ES6的类定义语法 基本知识点: ES6中定义类的方式, 就是ES5中定义类的语法糖,但虽然是语法糖,但是整体...

  • 二js类

    一类和函数 ES5中类和函数定义方式差不多, ES6中才出现了class 函数是一串逻辑的组合, 类是除了封装了逻...

网友评论

      本文标题:es6中的类和es5的类

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