如何深入探究JavaScript中的原型与原型链机制?
- 内容介绍
- 文章标签
- 相关推荐
本文共计1809个文字,预计阅读时间需要8分钟。
创建函数构造对象,我们首先使用构造函数创建一个对象:`function Person() {} var person=new Person(); person.name='Kevin'; console.log(person.name) // Kevin 在这个例子中,Person 就是一个构造函数,我们用它创建了一个对象实例 person。`
构造函数创建对象
我们先使用构造函数创建一个对象:
function Person() { } var person = new Person(); person.name = 'Kevin'; console.log(person.name) // Kevin
在这个例子中,Person 就是一个构造函数,我们使用 new 创建了一个实例对象 person。
很简单吧,接下来进入正题:
prototype
每个函数都有一个 prototype 属性,就是我们经常在各种例子中看到的那个 prototype ,比如:
function Person() { } // 虽然写在注释里,但是你要注意: // prototype是函数才会有的属性 Person.prototype.name = 'Kevin'; var person1 = new Person(); var person2 = new Person(); console.log(person1.name) // Kevin console.log(person2.name) // Kevin
那这个函数的 prototype 属性到底指向的是什么呢?是这个函数的原型吗?
其实,函数的 prototype 属性指向了一个对象,这个对象正是调用该构造函数而创建的实例的原型,也就是这个例子中的 person1 和 person2 的原型。
本文共计1809个文字,预计阅读时间需要8分钟。
创建函数构造对象,我们首先使用构造函数创建一个对象:`function Person() {} var person=new Person(); person.name='Kevin'; console.log(person.name) // Kevin 在这个例子中,Person 就是一个构造函数,我们用它创建了一个对象实例 person。`
构造函数创建对象
我们先使用构造函数创建一个对象:
function Person() { } var person = new Person(); person.name = 'Kevin'; console.log(person.name) // Kevin
在这个例子中,Person 就是一个构造函数,我们使用 new 创建了一个实例对象 person。
很简单吧,接下来进入正题:
prototype
每个函数都有一个 prototype 属性,就是我们经常在各种例子中看到的那个 prototype ,比如:
function Person() { } // 虽然写在注释里,但是你要注意: // prototype是函数才会有的属性 Person.prototype.name = 'Kevin'; var person1 = new Person(); var person2 = new Person(); console.log(person1.name) // Kevin console.log(person2.name) // Kevin
那这个函数的 prototype 属性到底指向的是什么呢?是这个函数的原型吗?
其实,函数的 prototype 属性指向了一个对象,这个对象正是调用该构造函数而创建的实例的原型,也就是这个例子中的 person1 和 person2 的原型。

