文章目录
  1. 1. Defined
  2. 2. Main()
    1. 2.1. Outline
    2. 2.2. Design Pattern: Constructor
    3. 2.3. Constructor VS Prototype
    4. 2.4. about this
  3. 3. Reference

Defined

本文由来: 构造函数和原型继承的区别是什么?
顺便推荐2017截止最新的动物电子书 - Learning JavaScript Design Patterns 作者Addy Osmani

Main()

Outline

  1. Design Pattern: Constructor
    this in constructor ???
  2. Design Pattern: Prototype
  3. this in prototype
  4. Constructor VS Prototype
    5.
    .. ES6 class realization with ES5

Design Pattern: Constructor

Constructor VS Prototype

https://www.thecodeship.com/web-development/methods-within-constructor-vs-prototype-in-javascript/

@aymanfarhat
In fact no classes in JavaScript (Before ES6), and what you may refer to as a class is known as a constructor. That is because JavaScript is not your classic class-based language but rather a prototype-based language.

about this

prototype - this | stackoverflow

Andress
The “this” keyword works a little bit diffrent in javascript than it does in other languages. “this” holds a reference to the object from which the function was called. If the function is not invoked as method on an object, this will refer to the global object (the “window” object).

MDN

1
2
3
4
5
6
7
8
9
10
11
function Mammal() {
this.isMammal = 'yes';
}

function MammalSpecies(speciesName) {
this.species = speciesName;
}

MammalSpecies.prototype = Mammal;

console.log(MammalSpecies.prototype.constructor);

Reference

Methods Within Constructor vs Prototype in Javascript
setPrototypeOf - MDN
JavaScript 实现类的继承
Understanding the difference between Object.create() and new SomeFunction() | stackoverflow

文章目录
  1. 1. Defined
  2. 2. Main()
    1. 2.1. Outline
    2. 2.2. Design Pattern: Constructor
    3. 2.3. Constructor VS Prototype
    4. 2.4. about this
  3. 3. Reference