JS基础——原型与原型链
前言 最近在整理一些js的基础知识刚好整理到原型 构造函数 每个构造函数(constructor)都有一个原型对象(prototype), 原型对象都包含一个指向构造函数的指针, 而实例(instance)都包含一个指向原型对象的内部指针. 先来看一个例子 function Person(name, age, job) { this.name = name this.age = age this.job = job this.sayName = function() { alert(this.name) } } var person1 = new Person('Zaxlct', 28, 'Engineer') var person2 = new Person('Mick', 23, 'Doctor') 上面的例子中 person1 和 person2 都是 Person 的实例。这两个实例都有一个 constructor (构造函数)属性,该属性(是一个指针)指向 Person。 即: console.log(person1.constructor == Person) //true console.log(person2.constructor == Person) //true prototype 每个构造函数都有一个 prototype 属性