---
title: "JavaScripte中的Prototype机制（一）"
author: "bbiao"
date: "2006-12-15T11:07:00Z"
tags: []
slug: "javascripte-prototype"
draft: false
lastmod: "2006-12-15T11:07:00Z"
id: 160403
url: "https://bbiao.iteye.com/blog/160403"
---

JavaScript不是一个真正的OO语言，但是通过提供prototype对象实现了一些OO的特性。本文是作者在使用JS中OO特性的一些心得体会。

下面我们首先来看一下如何在JavaScript中定义一个类（不使用prototype）：

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)function Base()  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        this.name = "name";  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        this.show = function()   
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)        ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                alert(this.name);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)        }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)}

以上这段代码中，我们声明了一个类Base，并且为该类添加了一个成员变量name和一个成员函数show，通过经下代码就可以实现生成该类的一个实例：

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)var base = new Base();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)base.show();

但是这种声明类的方法有一个明显的不足之处，每当新建该类的一个实例时，我们都会创建所有方法的一个副本，也就是说，所有类的show方法是在内存中的不同地址的，而且类的成员变量的声明与定义被放到了一起，比较容易产生混淆。下面，我们通过引入prototype机制来实现清晰的类定义。

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)function Base()  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    // Constructor here.  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    // ...  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)Base.prototype =   
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    name: "base",  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    show: function()  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)    ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        alert(this.name);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)    }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)}

现在我们来看new的执行过程：  
1、创建一个新的对象，并让this指针指向它；  
2、将函数的prototype对象的所有成员都赋给这个新的对象；  
3、执行函数体，对这个对象进行初始化操作；  
4、返回（1）中创建的对象。  
现在在new的执行过程当中，多了用prototype来初始化对象的过程，这也和prototype的字面意思相符，它是所对应类的实例的原形。这个初始化过程发生在函数体（constructor）执行之前，所以在函数体内部可以调用prototype中定义的属性和方法。