Javascript面向对象入门

Edit

学习笔记 javascript 面向对象

写这篇笔记,是阅读了以下这个系列三个网页,想摘录一些东西,以后也方便回忆。

作者由浅入深,娓娓道来,讲的很透彻。文中讲了很多种javascript实现面向对象的方法,和各自的优劣。我只记录作者认为最好的,和我认为最常见的。

在开始之前,有一些heads up。

  • JS本身是不支持OO的,但是运用其自身的各种特性,仍然能实现类的封装和继承,这是OO的精华部分
  • JS中所有物件都是对象
    • function定义的函数是Function类的对象
    • 字典是Object类的对象
    • 数组是Array类的对象
    • 简单变量是Number类对象

类封装的Prototype模式

看下面的code:

1.function Cat(name,color){
2. this.name = name;
3. this.color = color;
4.}
5.
6.Cat.prototype.type = "猫科动物";
7.Cat.prototype.eat = function(){alert("吃老鼠")};
8.  
9.var cat1 = new Cat("大毛","黄色");
10.var cat2 = new Cat("二毛","黑色");
11.
12.alert(cat1.type); // 猫科动物
13.cat1.eat(); // 吃老鼠

每个函数都有一个prototype上,每个prototype指向另一个对象。不变的属性,方法可以定义在这个prototype上,从而达到减少重复,节省内存的作用。

express中的model定义就是用的这种方式。

继承

继承有两种,通过构造函数继承和通过对象拷贝继承。javascript是面向函数编程,这个函数对于类定义来说其实就是构造函数。上面讲的prototype就有点继承的意思了。但我们的prototype并没有一个函数与之对应,即没有构造函数,所以它不能称其类,也没法实例化。所以不能称其为继承。

通过prototype继承

那要继承就要有构造函数

1.function Animal(){
2. this.species = "动物";
3.}
4.Cat.prototype = new Animal();
5.Cat.prototype.constructor = Cat;
6.var cat1 = new Cat("大毛","黄色");
7.alert(cat1.species); // 动物

这样就是真正的继承了,Cat继承自Animal。这里有一个小把戏:Cat.prototype.constructor = Cat;
这里牵涉到一个很饶人的小问题,也是很多人在讨论的,也是jsoo躲不开的问题,就是:

constructor的作用到底是什么?
首先,在js中,所有的物件皆为对象。
其次每一个对象都有他的构造函数,由object.constructor指明。
最后,所有object.constructor均等于object.prototype.constructor。看下面的REPL的代码:

1.> function Cat(){}
2.> function Animal(){}
3.> Cat.prototype=new Animal
4.> cat = new Cat()
5.Animal {} // 看这里的结果
6.> cat.constructor===Animal // 再看这一行
7.true
8.> Cat.constructor === Function
9.true

可见,如果修改了类的prototype,它自己对象的constructor就会指向其他的地方。问题来了:

这么做有什么问题呢?

这让我百思不得其解。引用知乎上一个回答:

constructor属性不影响任何JavaScript的内部属性。instanceof检测对象的原型链,通常你是无法修改的(不过某些引擎通过私有的proto属性暴露出来)。
constructor其实没有什么用处,只是JavaScript语言设计的历史遗留物。由于constructor属性是可以变更的,所以未必真的指向对象的构造函数,只是一个提示。不过,从编程习惯上,我们应该尽量让对象的constructor指向其构造函数,以维持这个惯例

用instanceof看不出什么差别,可能也印证了这位知乎大牛的观点。

1.> cat = new Cat()
2.Animal {}
3.> cat instanceof Cat
4.true
5.> cat instanceof Animal
6.true
7.> Cat.prototype.constructor=Cat
8.[Function: Cat]
9.> cat=new Cat()
10.Cat {}
11.> cat instanceof Cat
12.true
13.> cat instanceof Animal
14.true

再回到我们之前的例子Cat.prototype = new Animal();这一句也隐含的做了Cat.prototype.constructor = Animal。导致cat.constructor === Cat.prototype.constructor
看起来有点乱,cat看起来是由Animal来构造的了。
我的理解是:

一般使用不会有问题,当在一些高级应用场合,要用到反射的时候,如果constructor设置不对会引起不必要的麻烦。

根据prototype继承的方法,Javascript面向对象编程(二):构造函数的继承又提出了两种改进型的继承方法,分别是

  • 直接继承prototype
  • 利用空对象作为中介

直接继承prototype

类的prototype是这个类稳定的部分,当我们想继承的时候,其实就是想继承类中稳定的部分。所以这个方法就是直接继承父类的prototype:

1.function Animal(){
2. this.species = "动物";
3.}
4.Cat.prototype = Aninal.prototype;
5.Cat.prototype.constructor = Cat;

虽然听起来有理,不过真的这么做还是挺奇怪的。子类和父类的prototype指向了同一个对象(JSOO中,万物皆对象,和C++/Java中的对象概念有稍许区别)。
而对Cat.prototype.constructor的修改直接影响了Animal.constructor。

结论就是这个方法不好,不能用。

利用空对象作为中介

直接上代码:

1.var F = function(){};
2.F.prototype = Animal.prototype;
3.Cat.prototype = new F();
4.Cat.prototype.constructor = Cat;

这样的做法隔离了父类和子类。

  • 优点是节省内存
    • 因为F是空函数,定义Cat.prototype的时候,new的是F比new一个Animal肯定是要节省内存的。
  • 缺点是不太直观。

不过据说有些JS库(YUI)的OO就是用的这种模式。

使用方法:

1.function extend(Child, Parent) {
2. var F = function(){};
3. F.prototype = Parent.prototype;
4. Child.prototype = new F();
5. Child.prototype.constructor = Child;
6. Child.uber = Parent.prototype;
7.}
8.
9.extend(Cat,Animal);
10.var cat1 = new Cat("大毛","黄色");
11.alert(cat1.species); // 动物

这里用到了一个类的uber属性。这是什么?

意思是为子对象设一个uber属性,这个属性直接指向父对象的prototype属性。(uber是一个德语词,意思是”向上”、”上一层”。)这等于在子对象上打开一条通道,可以直接调用父对象的方法。这一行放在这里,只是为了实现继承的完备性,纯属备用性质。

我的理解,给子类能访问父类函数的机会。为什么叫uber,我觉得应该是约定俗成的。

1.> function Cat2(){}
2.> 'uber' in Cat2
3.false
4.>

最后还有一个另类,通过在构造函数里面拷贝父类的属性达到继承的效果,这个和后一章的继承方法有异曲同工之效。区别是这里是在构造函数里做拷贝。

1.function extend2(Child, Parent) {
2. var p = Parent.prototype;
3. var c = Child.prototype;
4. for (var i in p) {
5. c[i] = p[i];
6. }
7. c.uber = p;
8.}
9.
10.extend2(Cat, Animal);
11.var cat1 = new Cat("大毛","黄色");
12.alert(cat1.species); // 动物

注意Javascript面向对象编程(二):构造函数的继承中虽然没有提到,但这里仍然会有深拷贝,浅拷贝的问题。

通过拷贝继承

这里解决的问题,和标准的OO处理的问题不同。这里要处理的问题,是让两个普通对象之间有继承关系:

1.var Chinese = {
2. nation:'中国'
3.};
4.var Doctor ={
5. career:'医生'
6.}

注意这里都是普通的字典类型对象,并不像前一章说的是用function构造出来的类,再用new构造的对象。
现在要解决的问题,是’中国医生’要如何定义。这里很容易想到就是把Chinese里面的属性拷给Doctor就好了嘛。那这一章讲的就是这个内容。

object()方法

这个方法其实还是在用构造函数。

1.function object(o) {
2. function F() {}
3. F.prototype = o;
4. return new F();
5.}
6.
7.var Doctor = object(Chinese);
8.Doctor.career = '医生';
9.alert(Doctor.nation); //中国

根据上一章我们讲的构造函数的知识,可以知道:

  • Doctor是F类的对象
  • F类通过prototype继承了Chinese对象
  • 有下面的等式
1.> Doctor.constructor === Chinese.constructor
2.true

Doctor是F类的对象,F类的prototype是Chinese对象,Doctor的constructor指向他原型的contructor。
非要和构造函数扯上关系,是想对上一章的知识活学活用。作者把这个方法列在这里的用意,其实是这里的方法跟构造函数,constructor没半毛钱关系。只是单纯的将Chinese通过prototype传给Doctor

后面的方法就从形式上也和构造函数也没半毛钱关系了。

  • 浅拷贝
  • 深拷贝
    不讲浅拷贝,因为深拷贝包含了浅拷贝

深拷贝

其实拷贝对于解决这个问题,本身就是最直觉的方案。直接看code:

1.function deepCopy(p, c) {
2. var c = c || {};
3. for (var i in p) {
4. if (typeof p[i] === 'object') {
5. c[i] = (p[i].constructor === Array) ? [] : {};
6. deepCopy(p[i], c[i]);
7. } else {
8. c[i] = p[i];
9. }
10. }
11. return c;
12.}
13.var Doctor = deepCopy(Chinese);

要用深拷贝的原因是Chinese对象内部可能有成员的值是数组或其他对象。jQuery库用的就是这种继承方法。

%23Javascript%u9762%u5411%u5BF9%u8C61%u5165%u95E8%0A@%28%u5B66%u4E60%u7B14%u8BB0%29%5Bjavascript%2C%u9762%u5411%u5BF9%u8C61%5D%0A%0A%5BTOC%5D%0A%0A%u5199%u8FD9%u7BC7%u7B14%u8BB0%uFF0C%u662F%u9605%u8BFB%u4E86%u4EE5%u4E0B%u8FD9%u4E2A%u7CFB%u5217%u4E09%u4E2A%u7F51%u9875%uFF0C%u60F3%u6458%u5F55%u4E00%u4E9B%u4E1C%u897F%uFF0C%u4EE5%u540E%u4E5F%u65B9%u4FBF%u56DE%u5FC6%u3002%0A-%20%5BJavascript%20%u9762%u5411%u5BF9%u8C61%u7F16%u7A0B%uFF08%u4E00%uFF09%uFF1A%u5C01%u88C5%5D%28https%3A//www.evernote.com/shard/s24/nl/697376807/f579e44e-fb4a-4bc5-ac4e-4279f39cf00e/%29%0A-%20%5BJavascript%u9762%u5411%u5BF9%u8C61%u7F16%u7A0B%uFF08%u4E8C%uFF09%uFF1A%u6784%u9020%u51FD%u6570%u7684%u7EE7%u627F%5D%28https%3A//www.evernote.com/shard/s24/nl/697376807/9f56f8ec-b75a-469d-9320-d3d206766dc1/%29%0A-%20%5BJavascript%u9762%u5411%u5BF9%u8C61%u7F16%u7A0B%uFF08%u4E09%uFF09%uFF1A%u975E%u6784%u9020%u51FD%u6570%u7684%u7EE7%u627F%5D%28https%3A//www.evernote.com/shard/s24/nl/697376807/32cfbff4-0cef-44a2-adeb-6953cecc1158/%29%0A%0A%u4F5C%u8005%u7531%u6D45%u5165%u6DF1%uFF0C%u5A13%u5A13%u9053%u6765%uFF0C%u8BB2%u7684%u5F88%u900F%u5F7B%u3002%u6587%u4E2D%u8BB2%u4E86%u5F88%u591A%u79CDjavascript%u5B9E%u73B0%u9762%u5411%u5BF9%u8C61%u7684%u65B9%u6CD5%uFF0C%u548C%u5404%u81EA%u7684%u4F18%u52A3%u3002%u6211%u53EA%u8BB0%u5F55%u4F5C%u8005%u8BA4%u4E3A%u6700%u597D%u7684%uFF0C%u548C%u6211%u8BA4%u4E3A%u6700%u5E38%u89C1%u7684%u3002%0A%0A%u5728%u5F00%u59CB%u4E4B%u524D%uFF0C%u6709%u4E00%u4E9Bheads%20up%u3002%0A-%20JS%u672C%u8EAB%u662F%u4E0D%u652F%u6301OO%u7684%uFF0C%u4F46%u662F%u8FD0%u7528%u5176%u81EA%u8EAB%u7684%u5404%u79CD%u7279%u6027%uFF0C%u4ECD%u7136%u80FD%u5B9E%u73B0%u7C7B%u7684%u5C01%u88C5%u548C%u7EE7%u627F%uFF0C%u8FD9%u662FOO%u7684%u7CBE%u534E%u90E8%u5206%0A-%20JS%u4E2D%u6240%u6709%u7269%u4EF6%u90FD%u662F%u5BF9%u8C61%0A%09-%20function%u5B9A%u4E49%u7684%u51FD%u6570%u662FFunction%u7C7B%u7684%u5BF9%u8C61%0A%09-%20%u5B57%u5178%u662FObject%u7C7B%u7684%u5BF9%u8C61%0A%09-%20%u6570%u7EC4%u662FArray%u7C7B%u7684%u5BF9%u8C61%0A%09-%20%u7B80%u5355%u53D8%u91CF%u662FNumber%u7C7B%u5BF9%u8C61%0A%0A%23%23%u7C7B%u5C01%u88C5%u7684Prototype%u6A21%u5F0F%0A%u770B%u4E0B%u9762%u7684code%uFF1A%0A%60%60%60javascript%0Afunction%20Cat%28name%2Ccolor%29%7B%0A%09this.name%20%3D%20name%3B%0A%09this.color%20%3D%20color%3B%0A%7D%0A%0ACat.prototype.type%20%3D%20%22%u732B%u79D1%u52A8%u7269%22%3B%0ACat.prototype.eat%20%3D%20function%28%29%7Balert%28%22%u5403%u8001%u9F20%22%29%7D%3B%0A%u3000%u3000%0Avar%20cat1%20%3D%20new%20Cat%28%22%u5927%u6BDB%22%2C%22%u9EC4%u8272%22%29%3B%0Avar%20cat2%20%3D%20new%20Cat%28%22%u4E8C%u6BDB%22%2C%22%u9ED1%u8272%22%29%3B%0A%0Aalert%28cat1.type%29%3B%20//%20%u732B%u79D1%u52A8%u7269%0Acat1.eat%28%29%3B%20//%20%u5403%u8001%u9F20%0A%60%60%60%0A%u6BCF%u4E2A%u51FD%u6570%u90FD%u6709%u4E00%u4E2Aprototype%u4E0A%uFF0C%u6BCF%u4E2Aprototype%u6307%u5411%u53E6%u4E00%u4E2A%u5BF9%u8C61%u3002%u4E0D%u53D8%u7684%u5C5E%u6027%uFF0C%u65B9%u6CD5%u53EF%u4EE5%u5B9A%u4E49%u5728%u8FD9%u4E2Aprototype%u4E0A%uFF0C%u4ECE%u800C%u8FBE%u5230%u51CF%u5C11%u91CD%u590D%uFF0C%u8282%u7701%u5185%u5B58%u7684%u4F5C%u7528%u3002%0A%0Aexpress%u4E2D%u7684model%u5B9A%u4E49%u5C31%u662F%u7528%u7684%u8FD9%u79CD%u65B9%u5F0F%u3002%0A%0A%23%23%u7EE7%u627F%0A%u7EE7%u627F%u6709%u4E24%u79CD%uFF0C%u901A%u8FC7%u6784%u9020%u51FD%u6570%u7EE7%u627F%u548C%u901A%u8FC7%u5BF9%u8C61%u62F7%u8D1D%u7EE7%u627F%u3002javascript%u662F%u9762%u5411%u51FD%u6570%u7F16%u7A0B%uFF0C%u8FD9%u4E2A%u51FD%u6570%u5BF9%u4E8E%u7C7B%u5B9A%u4E49%u6765%u8BF4%u5176%u5B9E%u5C31%u662F%u6784%u9020%u51FD%u6570%u3002%u4E0A%u9762%u8BB2%u7684prototype%u5C31%u6709%u70B9%u7EE7%u627F%u7684%u610F%u601D%u4E86%u3002%u4F46%u6211%u4EEC%u7684prototype%u5E76%u6CA1%u6709%u4E00%u4E2A%u51FD%u6570%u4E0E%u4E4B%u5BF9%u5E94%uFF0C%u5373%u6CA1%u6709%u6784%u9020%u51FD%u6570%uFF0C%u6240%u4EE5%u5B83%u4E0D%u80FD%u79F0%u5176%u7C7B%uFF0C%u4E5F%u6CA1%u6CD5%u5B9E%u4F8B%u5316%u3002%u6240%u4EE5%u4E0D%u80FD%u79F0%u5176%u4E3A%u7EE7%u627F%u3002%0A%0A%23%23%23%u901A%u8FC7%u6784%u9020%u51FD%u6570%u7EE7%u627F%0A%23%23%23%23%u901A%u8FC7prototype%u7EE7%u627F%0A%u90A3%u8981%u7EE7%u627F%u5C31%u8981%u6709%u6784%u9020%u51FD%u6570%0A%60%60%60javascript%0Afunction%20Animal%28%29%7B%0A%09this.species%20%3D%20%22%u52A8%u7269%22%3B%0A%7D%0ACat.prototype%20%3D%20new%20Animal%28%29%3B%0ACat.prototype.constructor%20%3D%20Cat%3B%0Avar%20cat1%20%3D%20new%20Cat%28%22%u5927%u6BDB%22%2C%22%u9EC4%u8272%22%29%3B%0Aalert%28cat1.species%29%3B%20//%20%u52A8%u7269%0A%60%60%60%0A%u8FD9%u6837%u5C31%u662F%u771F%u6B63%u7684%u7EE7%u627F%u4E86%uFF0CCat%u7EE7%u627F%u81EAAnimal%u3002%u8FD9%u91CC%u6709%u4E00%u4E2A%u5C0F%u628A%u620F%uFF1A%60Cat.prototype.constructor%20%3D%20Cat%3B%60%0A%u8FD9%u91CC%u7275%u6D89%u5230%u4E00%u4E2A%u5F88%u9976%u4EBA%u7684%u5C0F%u95EE%u9898%uFF0C%u4E5F%u662F%u5F88%u591A%u4EBA%u5728%u8BA8%u8BBA%u7684%uFF0C%u4E5F%u662Fjsoo%u8EB2%u4E0D%u5F00%u7684%u95EE%u9898%uFF0C%u5C31%u662F%uFF1A%0A%3Econstructor%u7684%u4F5C%u7528%u5230%u5E95%u662F%u4EC0%u4E48%uFF1F%0A%u9996%u5148%uFF0C%u5728js%u4E2D%uFF0C%u6240%u6709%u7684%u7269%u4EF6%u7686%u4E3A%u5BF9%u8C61%u3002%0A%u5176%u6B21%u6BCF%u4E00%u4E2A%u5BF9%u8C61%u90FD%u6709%u4ED6%u7684%u6784%u9020%u51FD%u6570%uFF0C%u7531%60object.constructor%60%u6307%u660E%u3002%0A%u6700%u540E%uFF0C%u6240%u6709%60object.constructor%60%u5747%u7B49%u4E8E%60object.prototype.constructor%60%u3002%u770B%u4E0B%u9762%u7684REPL%u7684%u4EE3%u7801%uFF1A%0A%60%60%60javascript%0A%3E%20function%20Cat%28%29%7B%7D%0A%3E%20function%20Animal%28%29%7B%7D%0A%3E%20Cat.prototype%3Dnew%20Animal%0A%3E%20cat%20%3D%20new%20Cat%28%29%0AAnimal%20%7B%7D%20//%20%u770B%u8FD9%u91CC%u7684%u7ED3%u679C%0A%3E%20cat.constructor%3D%3D%3DAnimal%20//%20%u518D%u770B%u8FD9%u4E00%u884C%0Atrue%0A%3E%20Cat.constructor%20%3D%3D%3D%20Function%0Atrue%0A%60%60%60%0A%u53EF%u89C1%uFF0C%u5982%u679C%u4FEE%u6539%u4E86%u7C7B%u7684prototype%uFF0C%u5B83%u81EA%u5DF1%u5BF9%u8C61%u7684constructor%u5C31%u4F1A%u6307%u5411%u5176%u4ED6%u7684%u5730%u65B9%u3002%u95EE%u9898%u6765%u4E86%uFF1A%0A%3E%u8FD9%u4E48%u505A%u6709%u4EC0%u4E48%u95EE%u9898%u5462%uFF1F%0A%0A%u8FD9%u8BA9%u6211%u767E%u601D%u4E0D%u5F97%u5176%u89E3%u3002%u5F15%u7528%u77E5%u4E4E%u4E0A%u4E00%u4E2A%u56DE%u7B54%uFF1A%0A%3Econstructor%u5C5E%u6027%u4E0D%u5F71%u54CD%u4EFB%u4F55JavaScript%u7684%u5185%u90E8%u5C5E%u6027%u3002instanceof%u68C0%u6D4B%u5BF9%u8C61%u7684%u539F%u578B%u94FE%uFF0C%u901A%u5E38%u4F60%u662F%u65E0%u6CD5%u4FEE%u6539%u7684%uFF08%u4E0D%u8FC7%u67D0%u4E9B%u5F15%u64CE%u901A%u8FC7%u79C1%u6709%u7684__proto__%u5C5E%u6027%u66B4%u9732%u51FA%u6765%uFF09%u3002%0A%3E**constructor%u5176%u5B9E%u6CA1%u6709%u4EC0%u4E48%u7528%u5904%uFF0C%u53EA%u662FJavaScript%u8BED%u8A00%u8BBE%u8BA1%u7684%u5386%u53F2%u9057%u7559%u7269**%u3002%u7531%u4E8Econstructor%u5C5E%u6027%u662F%u53EF%u4EE5%u53D8%u66F4%u7684%uFF0C%u6240%u4EE5%u672A%u5FC5%u771F%u7684%u6307%u5411%u5BF9%u8C61%u7684%u6784%u9020%u51FD%u6570%uFF0C%u53EA%u662F%u4E00%u4E2A%u63D0%u793A%u3002**%u4E0D%u8FC7%uFF0C%u4ECE%u7F16%u7A0B%u4E60%u60EF%u4E0A%uFF0C%u6211%u4EEC%u5E94%u8BE5%u5C3D%u91CF%u8BA9%u5BF9%u8C61%u7684constructor%u6307%u5411%u5176%u6784%u9020%u51FD%u6570%uFF0C%u4EE5%u7EF4%u6301%u8FD9%u4E2A%u60EF%u4F8B**%u3002%0A%0A%u7528instanceof%u770B%u4E0D%u51FA%u4EC0%u4E48%u5DEE%u522B%uFF0C%u53EF%u80FD%u4E5F%u5370%u8BC1%u4E86%u8FD9%u4F4D%u77E5%u4E4E%u5927%u725B%u7684%u89C2%u70B9%u3002%0A%60%60%60javascript%0A%3E%20cat%20%3D%20new%20Cat%28%29%0AAnimal%20%7B%7D%0A%3E%20cat%20instanceof%20Cat%0Atrue%0A%3E%20cat%20instanceof%20Animal%0Atrue%0A%3E%20Cat.prototype.constructor%3DCat%0A%5BFunction%3A%20Cat%5D%0A%3E%20cat%3Dnew%20Cat%28%29%0ACat%20%7B%7D%0A%3E%20cat%20instanceof%20Cat%0Atrue%0A%3E%20cat%20instanceof%20Animal%0Atrue%0A%60%60%60%0A%0A%u518D%u56DE%u5230%u6211%u4EEC%u4E4B%u524D%u7684%u4F8B%u5B50%60Cat.prototype%20%3D%20new%20Animal%28%29%3B%60%u8FD9%u4E00%u53E5%u4E5F%u9690%u542B%u7684%u505A%u4E86%60Cat.prototype.constructor%20%3D%20Animal%60%u3002%u5BFC%u81F4%60cat.constructor%20%3D%3D%3D%20Cat.prototype.constructor%60%0A%u770B%u8D77%u6765%u6709%u70B9%u4E71%uFF0Ccat%u770B%u8D77%u6765%u662F%u7531Animal%u6765%u6784%u9020%u7684%u4E86%u3002%0A%u6211%u7684%u7406%u89E3%u662F%uFF1A%0A%3E%u4E00%u822C%u4F7F%u7528%u4E0D%u4F1A%u6709%u95EE%u9898%uFF0C%u5F53%u5728%u4E00%u4E9B%u9AD8%u7EA7%u5E94%u7528%u573A%u5408%uFF0C%u8981%u7528%u5230%u53CD%u5C04%u7684%u65F6%u5019%uFF0C%u5982%u679Cconstructor%u8BBE%u7F6E%u4E0D%u5BF9%u4F1A%u5F15%u8D77%u4E0D%u5FC5%u8981%u7684%u9EBB%u70E6%u3002%0A%0A%u6839%u636Eprototype%u7EE7%u627F%u7684%u65B9%u6CD5%uFF0C%5BJavascript%u9762%u5411%u5BF9%u8C61%u7F16%u7A0B%uFF08%u4E8C%uFF09%uFF1A%u6784%u9020%u51FD%u6570%u7684%u7EE7%u627F%5D%28https%3A//www.evernote.com/shard/s24/nl/697376807/9f56f8ec-b75a-469d-9320-d3d206766dc1/%29%u53C8%u63D0%u51FA%u4E86%u4E24%u79CD%u6539%u8FDB%u578B%u7684%u7EE7%u627F%u65B9%u6CD5%uFF0C%u5206%u522B%u662F%0A-%20%u76F4%u63A5%u7EE7%u627Fprototype%0A-%20%u5229%u7528%u7A7A%u5BF9%u8C61%u4F5C%u4E3A%u4E2D%u4ECB%0A%0A%23%23%23%23%u76F4%u63A5%u7EE7%u627Fprototype%0A%u7C7B%u7684prototype%u662F%u8FD9%u4E2A%u7C7B%u7A33%u5B9A%u7684%u90E8%u5206%uFF0C%u5F53%u6211%u4EEC%u60F3%u7EE7%u627F%u7684%u65F6%u5019%uFF0C%u5176%u5B9E%u5C31%u662F%u60F3%u7EE7%u627F%u7C7B%u4E2D%u7A33%u5B9A%u7684%u90E8%u5206%u3002%u6240%u4EE5%u8FD9%u4E2A%u65B9%u6CD5%u5C31%u662F%u76F4%u63A5%u7EE7%u627F%u7236%u7C7B%u7684prototype%uFF1A%0A%60%60%60javascript%0Afunction%20Animal%28%29%7B%0A%09this.species%20%3D%20%22%u52A8%u7269%22%3B%0A%7D%0ACat.prototype%20%3D%20Aninal.prototype%3B%0ACat.prototype.constructor%20%3D%20Cat%3B%0A%60%60%60%0A%u867D%u7136%u542C%u8D77%u6765%u6709%u7406%uFF0C%u4E0D%u8FC7%u771F%u7684%u8FD9%u4E48%u505A%u8FD8%u662F%u633A%u5947%u602A%u7684%u3002%u5B50%u7C7B%u548C%u7236%u7C7B%u7684prototype%u6307%u5411%u4E86%u540C%u4E00%u4E2A**%u5BF9%u8C61**%28JSOO%u4E2D%uFF0C%u4E07%u7269%u7686%u5BF9%u8C61%uFF0C%u548CC++/Java%u4E2D%u7684%u5BF9%u8C61%u6982%u5FF5%u6709%u7A0D%u8BB8%u533A%u522B%29%u3002%0A%u800C%u5BF9Cat.prototype.constructor%u7684%u4FEE%u6539%u76F4%u63A5%u5F71%u54CD%u4E86Animal.constructor%u3002%0A%0A%u7ED3%u8BBA%u5C31%u662F%u8FD9%u4E2A%u65B9%u6CD5%u4E0D%u597D%uFF0C%u4E0D%u80FD%u7528%u3002%0A%0A%23%23%23%23%u5229%u7528%u7A7A%u5BF9%u8C61%u4F5C%u4E3A%u4E2D%u4ECB%0A%u76F4%u63A5%u4E0A%u4EE3%u7801%uFF1A%0A%60%60%60javascript%0Avar%20F%20%3D%20function%28%29%7B%7D%3B%0AF.prototype%20%3D%20Animal.prototype%3B%0ACat.prototype%20%3D%20new%20F%28%29%3B%0ACat.prototype.constructor%20%3D%20Cat%3B%0A%60%60%60%0A%u8FD9%u6837%u7684%u505A%u6CD5%u9694%u79BB%u4E86%u7236%u7C7B%u548C%u5B50%u7C7B%u3002%0A-%20%u4F18%u70B9%u662F%u8282%u7701%u5185%u5B58%0A%09-%20%u56E0%u4E3AF%u662F%u7A7A%u51FD%u6570%uFF0C%u5B9A%u4E49Cat.prototype%u7684%u65F6%u5019%uFF0Cnew%u7684%u662FF%u6BD4new%u4E00%u4E2AAnimal%u80AF%u5B9A%u662F%u8981%u8282%u7701%u5185%u5B58%u7684%u3002%0A-%20%u7F3A%u70B9%u662F%u4E0D%u592A%u76F4%u89C2%u3002%0A%0A%u4E0D%u8FC7%u636E%u8BF4%u6709%u4E9BJS%u5E93%uFF08YUI%uFF09%u7684OO%u5C31%u662F%u7528%u7684%u8FD9%u79CD%u6A21%u5F0F%u3002%0A%0A%u4F7F%u7528%u65B9%u6CD5%uFF1A%0A%60%60%60javascript%0Afunction%20extend%28Child%2C%20Parent%29%20%7B%0A%09var%20F%20%3D%20function%28%29%7B%7D%3B%0A%09F.prototype%20%3D%20Parent.prototype%3B%0A%09Child.prototype%20%3D%20new%20F%28%29%3B%0A%09Child.prototype.constructor%20%3D%20Child%3B%0A%09Child.uber%20%3D%20Parent.prototype%3B%0A%7D%0A%0Aextend%28Cat%2CAnimal%29%3B%0Avar%20cat1%20%3D%20new%20Cat%28%22%u5927%u6BDB%22%2C%22%u9EC4%u8272%22%29%3B%0Aalert%28cat1.species%29%3B%20//%20%u52A8%u7269%0A%60%60%60%0A%0A%u8FD9%u91CC%u7528%u5230%u4E86%u4E00%u4E2A%u7C7B%u7684uber%u5C5E%u6027%u3002%u8FD9%u662F%u4EC0%u4E48%uFF1F%0A%3E%u610F%u601D%u662F%u4E3A%u5B50%u5BF9%u8C61%u8BBE%u4E00%u4E2Auber%u5C5E%u6027%uFF0C%u8FD9%u4E2A%u5C5E%u6027%u76F4%u63A5%u6307%u5411%u7236%u5BF9%u8C61%u7684prototype%u5C5E%u6027%u3002%uFF08uber%u662F%u4E00%u4E2A%u5FB7%u8BED%u8BCD%uFF0C%u610F%u601D%u662F%22%u5411%u4E0A%22%u3001%22%u4E0A%u4E00%u5C42%22%u3002%uFF09%u8FD9%u7B49%u4E8E%u5728%u5B50%u5BF9%u8C61%u4E0A%u6253%u5F00%u4E00%u6761%u901A%u9053%uFF0C%u53EF%u4EE5%u76F4%u63A5%u8C03%u7528%u7236%u5BF9%u8C61%u7684%u65B9%u6CD5%u3002%u8FD9%u4E00%u884C%u653E%u5728%u8FD9%u91CC%uFF0C%u53EA%u662F%u4E3A%u4E86%u5B9E%u73B0%u7EE7%u627F%u7684%u5B8C%u5907%u6027%uFF0C%u7EAF%u5C5E%u5907%u7528%u6027%u8D28%u3002%0A%0A%u6211%u7684%u7406%u89E3%uFF0C%u7ED9%u5B50%u7C7B%u80FD%u8BBF%u95EE%u7236%u7C7B%u51FD%u6570%u7684%u673A%u4F1A%u3002%u4E3A%u4EC0%u4E48%u53EBuber%uFF0C%u6211%u89C9%u5F97%u5E94%u8BE5%u662F%u7EA6%u5B9A%u4FD7%u6210%u7684%u3002%0A%0A%60%60%60javascript%0A%3E%20function%20Cat2%28%29%7B%7D%0A%3E%20%27uber%27%20in%20Cat2%0Afalse%0A%3E%0A%60%60%60%0A%0A%u6700%u540E%u8FD8%u6709%u4E00%u4E2A%u53E6%u7C7B%uFF0C%u901A%u8FC7%u5728%u6784%u9020%u51FD%u6570%u91CC%u9762%u62F7%u8D1D%u7236%u7C7B%u7684%u5C5E%u6027%u8FBE%u5230%u7EE7%u627F%u7684%u6548%u679C%uFF0C%u8FD9%u4E2A%u548C%u540E%u4E00%u7AE0%u7684%u7EE7%u627F%u65B9%u6CD5%u6709%u5F02%u66F2%u540C%u5DE5%u4E4B%u6548%u3002%u533A%u522B%u662F%u8FD9%u91CC%u662F%u5728%u6784%u9020%u51FD%u6570%u91CC%u505A%u62F7%u8D1D%u3002%0A%60%60%60javascript%0Afunction%20extend2%28Child%2C%20Parent%29%20%7B%0A%09var%20p%20%3D%20Parent.prototype%3B%0A%09var%20c%20%3D%20Child.prototype%3B%0A%09for%20%28var%20i%20in%20p%29%20%7B%0A%09%09c%5Bi%5D%20%3D%20p%5Bi%5D%3B%0A%09%7D%0A%09c.uber%20%3D%20p%3B%0A%7D%0A%0Aextend2%28Cat%2C%20Animal%29%3B%0Avar%20cat1%20%3D%20new%20Cat%28%22%u5927%u6BDB%22%2C%22%u9EC4%u8272%22%29%3B%0Aalert%28cat1.species%29%3B%20//%20%u52A8%u7269%0A%60%60%60%0A%u6CE8%u610F%5BJavascript%u9762%u5411%u5BF9%u8C61%u7F16%u7A0B%uFF08%u4E8C%uFF09%uFF1A%u6784%u9020%u51FD%u6570%u7684%u7EE7%u627F%5D%28https%3A//www.evernote.com/shard/s24/nl/697376807/9f56f8ec-b75a-469d-9320-d3d206766dc1/%29%u4E2D%u867D%u7136%u6CA1%u6709%u63D0%u5230%uFF0C%u4F46%u8FD9%u91CC%u4ECD%u7136%u4F1A%u6709%u6DF1%u62F7%u8D1D%uFF0C%u6D45%u62F7%u8D1D%u7684%u95EE%u9898%u3002%0A%23%23%23%u901A%u8FC7%u62F7%u8D1D%u7EE7%u627F%0A%u8FD9%u91CC%u89E3%u51B3%u7684%u95EE%u9898%uFF0C%u548C%u6807%u51C6%u7684OO%u5904%u7406%u7684%u95EE%u9898%u4E0D%u540C%u3002%u8FD9%u91CC%u8981%u5904%u7406%u7684%u95EE%u9898%uFF0C%u662F%u8BA9%u4E24%u4E2A%u666E%u901A%u5BF9%u8C61%u4E4B%u95F4%u6709%u7EE7%u627F%u5173%u7CFB%uFF1A%0A%60%60%60javascript%0Avar%20Chinese%20%3D%20%7B%0A%09nation%3A%27%u4E2D%u56FD%27%0A%7D%3B%0Avar%20Doctor%20%3D%7B%0A%09career%3A%27%u533B%u751F%27%0A%7D%0A%60%60%60%0A%u6CE8%u610F%u8FD9%u91CC%u90FD%u662F%u666E%u901A%u7684%u5B57%u5178%u7C7B%u578B%u5BF9%u8C61%uFF0C%u5E76%u4E0D%u50CF%u524D%u4E00%u7AE0%u8BF4%u7684%u662F%u7528function%u6784%u9020%u51FA%u6765%u7684%u7C7B%uFF0C%u518D%u7528new%u6784%u9020%u7684%u5BF9%u8C61%u3002%0A%u73B0%u5728%u8981%u89E3%u51B3%u7684%u95EE%u9898%uFF0C%u662F%27%u4E2D%u56FD%u533B%u751F%27%u8981%u5982%u4F55%u5B9A%u4E49%u3002%u8FD9%u91CC%u5F88%u5BB9%u6613%u60F3%u5230%u5C31%u662F%u628AChinese%u91CC%u9762%u7684%u5C5E%u6027%u62F7%u7ED9Doctor%u5C31%u597D%u4E86%u561B%u3002%u90A3%u8FD9%u4E00%u7AE0%u8BB2%u7684%u5C31%u662F%u8FD9%u4E2A%u5185%u5BB9%u3002%0A%0A%23%23%23%23object%28%29%u65B9%u6CD5%0A%u8FD9%u4E2A%u65B9%u6CD5%u5176%u5B9E%u8FD8%u662F%u5728%u7528%u6784%u9020%u51FD%u6570%u3002%0A%60%60%60javascript%0Afunction%20object%28o%29%20%7B%0A%09function%20F%28%29%20%7B%7D%0A%09F.prototype%20%3D%20o%3B%0A%09return%20new%20F%28%29%3B%0A%7D%0A%0Avar%20Doctor%20%3D%20object%28Chinese%29%3B%0ADoctor.career%20%3D%20%27%u533B%u751F%27%3B%0Aalert%28Doctor.nation%29%3B%20//%u4E2D%u56FD%0A%60%60%60%0A%u6839%u636E%u4E0A%u4E00%u7AE0%u6211%u4EEC%u8BB2%u7684%u6784%u9020%u51FD%u6570%u7684%u77E5%u8BC6%uFF0C%u53EF%u4EE5%u77E5%u9053%uFF1A%0A-%20Doctor%u662FF%u7C7B%u7684%u5BF9%u8C61%0A-%20F%u7C7B%u901A%u8FC7prototype%u7EE7%u627F%u4E86Chinese%u5BF9%u8C61%0A-%20%u6709%u4E0B%u9762%u7684%u7B49%u5F0F%0A%60%60%60javascript%0A%3E%20Doctor.constructor%20%3D%3D%3D%20Chinese.constructor%0Atrue%0A%60%60%60%0ADoctor%u662FF%u7C7B%u7684%u5BF9%u8C61%uFF0CF%u7C7B%u7684prototype%u662FChinese%u5BF9%u8C61%uFF0CDoctor%u7684constructor%u6307%u5411%u4ED6%u539F%u578B%u7684contructor%u3002%0A%u975E%u8981%u548C%u6784%u9020%u51FD%u6570%u626F%u4E0A%u5173%u7CFB%uFF0C%u662F%u60F3%u5BF9%u4E0A%u4E00%u7AE0%u7684%u77E5%u8BC6%u6D3B%u5B66%u6D3B%u7528%u3002%u4F5C%u8005%u628A%u8FD9%u4E2A%u65B9%u6CD5%u5217%u5728%u8FD9%u91CC%u7684%u7528%u610F%uFF0C%u5176%u5B9E%u662F%u8FD9%u91CC%u7684%u65B9%u6CD5%u8DDF%u6784%u9020%u51FD%u6570%uFF0Cconstructor%u6CA1%u534A%u6BDB%u94B1%u5173%u7CFB%u3002**%u53EA%u662F%u5355%u7EAF%u7684%u5C06Chinese%u901A%u8FC7prototype%u4F20%u7ED9Doctor**%u3002%0A%0A%u540E%u9762%u7684%u65B9%u6CD5%u5C31%u4ECE%u5F62%u5F0F%u4E0A%u4E5F%u548C%u6784%u9020%u51FD%u6570%u4E5F%u6CA1%u534A%u6BDB%u94B1%u5173%u7CFB%u4E86%u3002%0A-%20%u6D45%u62F7%u8D1D%0A-%20%u6DF1%u62F7%u8D1D%0A%u4E0D%u8BB2%u6D45%u62F7%u8D1D%uFF0C%u56E0%u4E3A%u6DF1%u62F7%u8D1D%u5305%u542B%u4E86%u6D45%u62F7%u8D1D%0A%0A%23%23%23%23%u6DF1%u62F7%u8D1D%0A%u5176%u5B9E%u62F7%u8D1D%u5BF9%u4E8E%u89E3%u51B3%u8FD9%u4E2A%u95EE%u9898%uFF0C%u672C%u8EAB%u5C31%u662F%u6700%u76F4%u89C9%u7684%u65B9%u6848%u3002%u76F4%u63A5%u770Bcode%uFF1A%0A%60%60%60javascript%0Afunction%20deepCopy%28p%2C%20c%29%20%7B%0A%09var%20c%20%3D%20c%20%7C%7C%20%7B%7D%3B%0A%09for%20%28var%20i%20in%20p%29%20%7B%0A%09%09if%20%28typeof%20p%5Bi%5D%20%3D%3D%3D%20%27object%27%29%20%7B%0A%09%09%09c%5Bi%5D%20%3D%20%28p%5Bi%5D.constructor%20%3D%3D%3D%20Array%29%20%3F%20%5B%5D%20%3A%20%7B%7D%3B%0A%09%09%09deepCopy%28p%5Bi%5D%2C%20c%5Bi%5D%29%3B%0A%09%09%7D%20else%20%7B%0A%09%09%09c%5Bi%5D%20%3D%20p%5Bi%5D%3B%0A%09%09%7D%0A%09%7D%0A%09return%20c%3B%0A%7D%0Avar%20Doctor%20%3D%20deepCopy%28Chinese%29%3B%0A%60%60%60%0A%u8981%u7528%u6DF1%u62F7%u8D1D%u7684%u539F%u56E0%u662FChinese%u5BF9%u8C61%u5185%u90E8%u53EF%u80FD%u6709%u6210%u5458%u7684%u503C%u662F%u6570%u7EC4%u6216%u5176%u4ED6%u5BF9%u8C61%u3002jQuery%u5E93%u7528%u7684%u5C31%u662F%u8FD9%u79CD%u7EE7%u627F%u65B9%u6CD5%u3002%0A%0A