JavaScript学习总结(十七)——Javascript原型链的原理

一、JavaScript原型链

  ECMAScript中描述了原型链的概念,并将原型链作为实现继承的主要方法。其基本思想是利用原型让一个引用类型继承另一个引用类型的属性和方法。在JavaScript中,用 __proto__ 属性来表示一个对象的原型链。当查找一个对象的属性时,JavaScript 会向上遍历原型链,直到找到给定名称的属性为止!

比如现在有如下的代码:

扩展Object类,添加Clone和Extend方法

JavaScript学习总结(十七)——Javascript原型链的原理
 1 /*扩展Object类,添加Clone,JS实现克隆的方法*/
2 Object.prototype.Clone = function(){
3 var objClone;
4 if (this.constructor == Object){
5 objClone = new this.constructor();
6 }else{
7 objClone = new this.constructor(this.valueOf());
8 }
9 for(var key in this){
10 if ( objClone[key] != this[key] ){
11 if ( typeof(this[key]) == 'object' ){
12 objClone[key] = this[key].Clone();
13 }else{
14 objClone[key] = this[key];
15 }
16 }
17 }
18 objClone.toString = this.toString;
19 objClone.valueOf = this.valueOf;
20 return objClone;
21 }
22
23 /*扩展Object类,添加Extend方法来实现JS继承, 目标对象将拥有源对象的所有属性和方法*/
24 Object.prototype.Extend = function (objDestination, objSource) {
25 for (var key in objSource) {
26 if (objSource.hasOwnProperty(key) && objDestination[key] === undefined) {
27 objDestination[key] = objSource[key];
28 }
29 }
30 return objDestination;
31 }
JavaScript学习总结(十七)——Javascript原型链的原理

定义Person类

1 /*定义一个Person类*/
2 function Person(_name,_age){
3 this.name = _name;
4 this.age = _age;
5 }

  在JavaScript中,Object类是所有类的父类,所以Person类从Object类继承,继承了Object类的所有public属性和public方法,包括Object类新添加的Clone和Extend方法

可以用如下的代码证明,Person类确实是继承了Object类

JavaScript学习总结(十七)——Javascript原型链的原理
 1 document.write("<pre>");
2
3 var p = new Person("孤傲苍狼",24);//创建一个人,名字是孤傲苍狼
4 var cloneP = p.Clone();//p调用在Object类中定义的Clone方法来克隆自己,如果能得到一个cloneP,那就证明了Person类确实是继承了Object类,所以就拥有了Clone
5 document.writeln("p是使用Person类以构造函数的方式创建出来的对象,p.name = "+p.name+",p.age = "+p.age);
6 document.writeln("cloneP是p调用Clone方法克隆出来的对象,cloneP.name = "+cloneP.name+",cloneP.age = "+cloneP.age);
7 document.writeln("cloneP对象和p对象是两个相互独立的对象,这两个对象的内存地址肯定是不相等,p == cloneP的结果是:"+(p == cloneP));
8 cloneP.name="白虎神皇";//修改cloneP的名字
9 document.writeln("cloneP的name被修改了,cloneP.name = "+cloneP.name);
10 document.writeln("cloneP的name修改了,但是不影响到p,p.name = "+p.name);
11
12 document.write("</pre>");
JavaScript学习总结(十七)——Javascript原型链的原理

运行结果:

  aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAsEAAABXCAIAAABqcw9UAAAUhklEQVR4nO1b27Ijqw5b///TfR52VQ6DLVkGOlfpIdUNxpYvGNYk83cZhmEYhmH08fdqAoZhGIZhfCR8hzAMwzAMYwW+QxiGYRiGsQLfIQzDMAzDWIHvEIZhGIZhrMB3CMMwDMMwVuA7hGEYhmEYK/AdwjAMwzCMFbTvEH8BRDJ9LhWWml+C1B2dZCqph0jR1jKdTu1nsyWjy6PZtVVdGV24GyVR+bvthXdD2i7iJtWrRZR0Xgxj5Q5BXtEU2XVo677h/py84NcCJVC674rpq3MhG/WUPVFP+oJY5JPOltEePdXvowfvEGtRKuV1X74bPMVcHlUX2S9Rc6rkZ9NhGP/hrn+HQK08Lom7NH0uLT4ThIPiDhJQoiTGv6Q6JqiUnGyhDltC4RnF4pQi3NWJxER31qKEmHN3fhx/4frLQ0fKIJUnkmlyy9r+DhBPf8F9A+Guf4cQxS68dYmGJ5csP0jiITGtRePj2m5UxQgQPZF/6VGkKqa1tN6NJ4kq0Z/SK90hoSAKlSghto9ZJT6/hjTOKB1TxMoA8tgSi1+fEb4lv959g+CWf4dINy2SFxslKeKnIe1fSEYZn/TEhwtc/7kVvc+Okqn1SZveOrs54vGZgoAChXSKU2XulLyXUSrjvMzzF6DX9vT8yIuY5dIuaVbfB1R+PKTGLwDeAJb3Rrq3lW5OnrnYxHNsFq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lmMB15Kg8iLLfWII907RItAy4XyGEMCiBWpASMNDortVJBlffK8E4vOkWH8IN79DvH8PjVdHRS7sSk/5wLRvUN0rzhILKoqyUyfKMhRYXpc/TimUKMQxYJMw040l3b1ijIM4yvB/uBIW0PaqqYjBJ1AUWH6nLbIVF40RDpdlIyfU8dEIUI8I/a77WTxbDcn8dfXplTRJ9GQxnbHU335HQWmUCqdmkoujeREPl0S1ZLXOKXLG4bxrSj+HLmqTpFKTu21lJz6ZhxMlXQNXaH3ITfjyaH0XMKzXLgGEpbxuQRZSwhHJSRK/BMRQGKkPhXoy28qsGWk+6KMZFrbSLNSIQ8lRMwwjF9AfYfgU+WRMw7GdkNOHf5ADE3mxtnY4tMOOAmXPTpdSMDDq/diJSxd6+iVu58GcJyKIY2xSs+kNKSkwJBrul+i5E6BbYIbTcVIPFGFp4SjElSEhmH8Ap56hxANkSXiIRc1tLpe2me75Al4ePVejM6qMqqpZCvauh506pQnX1yeykx+RccjysyWkpsFFq2U6UtZcXOiWJRHYsRZvWgNw/gm1N2kPEji89qgMrtpnTTT+Pz45D06CuhnwMLsmuRVxXxBf3RfXNIdiYPl0c6hX0FuKrBNRCv8ptW9Q5Cabyk0DOPrUfxBPDXB9E+laSR9VSRTQ+PgFZpyaWhS9fjkHBAlPoieo2bSc1vtWBee3N/XP50fouY0hnEEZb8lw2mky5GVKLBZYDuYbF04BaLYJM8Ho5JuURmG8U14x50/db04/kwCygGQyvBrxBGMJ1Z6pEWLXLLrwnRYioQnMhOxdNXZSviCA0+prpgdVBXTEl4eafbjq2EYvwDveePDIF7sblpuGIZhPOBOahiGYRjGCnyHMAzDMAxjBb5DGIZhGIaxAt8hDMMwDMNYge8QhmEYhmGs4H8qe9YVTFgiRwAAAABJRU5ErkJggg==" alt="" />

  那么Person类通过神马方式来继承Object类的呢,是使用原型(prototye)的方式继承的:

1  /*定义一个Person类*/
2 function Person(_name,_age){
3 this.name = _name;
4 this.age = _age;
5 }
6 Person.prototype = new Object();//让Person类继承Object类

  由于JavaScript规定,任何类都继承自Object类,所以"Person.prototype = new Object();//让Person类继承Object类"即使我们不写,我猜想JavaScript引擎也会自动帮我们加上这句话,或者是使用"Person.prototype =Object.prototype;"这种方式,让Person类去继承Object类。"Person.prototype = new Object();",其实这样就相当于Object对象是Person的一个原型,这样就相当于了把Object对象的属性和方法复制到了Person上了。

二、new运算符是如何工作的

  我们先看看这样一段代码:

1 var p  = new Person("孤傲苍狼",24);//创建一个人,名字是孤傲苍狼

  很简单的一段代码,我们来看看这个new究竟做了什么?我们可以把new的过程拆分成以下三步:

  1.var p={}; 初始化一个对象p。

  2. p.__proto__=Person.prototype;,将对象p的 __proto__ 属性设置为 Person.prototype

  3.Person.call(p,"孤傲苍狼",24);调用构造函数Person来初始化p。

关键在于第二步,我们来证明一下:

1 var p  = new Person("孤傲苍狼",24);//创建一个人,名字是孤傲苍狼
2 alert("p.__proto__ === Person.prototype的结果是:"+(p.__proto__ === Person.prototype));

在火狐下的运行结果是:

  aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAUMAAACNCAIAAACixI8gAAAJJUlEQVR4nO3cfWtb1x3A8fsq8g5C3oTfgv/OGxBsc6wICbN/uoVtJLCQtdSJ3GRTCy1dxgJVMqUeM0oJtCOxrTihWwWlbhLbqWXZkvV4JT/D7R8HH07vg2JbV4r00/fDwTjylXTv0f3qXIlSywEwzJaXv//jH35rvevdANCteCxCycDQo2RAAkoGJKBkQAJKBiSgZEACSgYkoGRAAkoGJKBkQAJKBiSgZEACSgYkoGRAAkoGJKBkQAJKBiSgZEACSgYkoGRAAkoGJKBkQAJKBiSgZEACSgYkoGRAAkoGJKBkQAJKBiSgZEACSgYkoGRAAkoGJKBkQAJKxoBKJBKlUinon4pl/eLszWaz+XzedYsVIJvNmo/jqweH1SuU3CeJREKfH66zDV6lUimVSgX9U8lms2aNTkDJepug3x3PO4KSSCS6OIJ+G+6SE4nEsFShl5RSqWRZlnd5GXAnmeqwXo58Pm8ujKVSybW0qtnT+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ztxWKCUvLj5pNiuTkxPqFtXq5OSEZVmLi0/0Bmb2pu7DViWzJmO0qJKTs5vxWKTRqAgY9fr2zOzmzOwmJWOEqJKnM8XpzEajsS1g1Ovl6UyRNRmjRZX8wYONeCxSr5cFjFpt64MHG+8/KFAyRogq+UZ6/UZ6vV4vCRi16uaN9DprMkaLKvl6ej0ei5TLhe1yYbu8MbyjXC5sba5dT69fT69TMkbI5sZP58avqFM/HotI+nlu/Mru7m6400XJGFDlreKv/vz5hfFr47//+jd/qooZF8avXfrL3b29vXCni5IxoKrVSr1WuvK3+8L+z/XR9/+xs9PY398Pd7ooGQPKtu1Wq7W7u7u/v3/Qe0e9pJ/l8PDw8PDw4OCA/0suAB+UDEhAyYAElAxIQMmABJQMSEDJgASUDEhAyYAElAxIQMmABJQMSEDJgASUDEhAyYAElAxIEI9FfgYYbxAVKf7raAAAAABJRU5ErkJggg==" alt="" />

这段代码会返回true。说明我们步骤2的正确。

  注意:__proto__这个属性只有在firefox或者chrome浏览器中才是公开允许访问的,因此,其他基于IE内核的浏览器是不会返回true的。

  那么__proto__是什么?在这里简单地说下。每个对象都会在其内部初始化一个属性,就是 __proto__,当我们访问一个对象的属性时,如果这个对象内部不存在这个属性,那么他就会去__proto__里找这个属性,这个 __proto__又会有自己的__proto__,于是就这样一直找下去,也就是我们平时所说的原型链的概念。

  按照标准,__proto__是不对外公开的,也就是说是个私有属性,在IE下是无法访问__proto__属性的,但是Firefox的引擎将他暴露了出来成为了一个公有的属性,我们可以对外访问和设置。

好,概念说清了,让我们看一下下面这些代码:

JavaScript学习总结(十七)——Javascript原型链的原理
1 <script type="text/javascript">
2 var Person = function () { };
3 Person.prototype.Say = function () {
4 alert("Person say");
5 }
6 var p = new Person();
7 p.Say();
8 </script>
JavaScript学习总结(十七)——Javascript原型链的原理

  这段代码很简单,我们看下为什么p可以访问Person的Say。

  首先

1 var p=new Person();

  可以得出

1 p.__proto__=Person.prototype

  那么当我们调用p.Say()时,首先p中没有Say这个属性, 于是,他就需要到他的__proto__中去找,也就是Person.prototype,而我们在上面定义了

1 Person.prototype.Say=function(){
2 alert("Person say");
3 };

   于是,就找到了这个方法。

 接下来,让我们看个更复杂的。

JavaScript学习总结(十七)——Javascript原型链的原理
 1 <script type="text/javascript">
2 var Person = function () { };
3 Person.prototype.Say = function () {
4 alert("Person say");
5 }
6 Person.prototype.Salary = 50000;
7 var Programmer = function () { };
8 Programmer.prototype = new Person();//让程序员类从人这个类继承
9 Programmer.prototype.WriteCode = function () {
10 alert("programmer writes code");
11 };
12 Programmer.prototype.Salary = 500;
13 var p = new Programmer();
14 p.Say();
15 p.WriteCode();
16 alert(p.Salary);
17 </script>
JavaScript学习总结(十七)——Javascript原型链的原理

  我们来做这样的推导:

1 var p=new Programmer();

  可以得出

1 p.__proto__=Programmer.prototype;

  而在上面我们指定了

1 Programmer.prototype=new Person();

  我们来这样拆分,

1 var p1=new Person();
2 Programmer.prototype=p1;

  那么:

1 p1.__proto__=Person.prototype;
2 Programmer.prototype.__proto__=Person.prototype;

  由根据上面得到

1 p.__proto__=Programmer.prototype

  可以得到:

1 p.__proto__.__proto__=Person.prototype

  好,算清楚了之后我们来看上面的结果,p.Say()。由于p没有Say这个属性,于是去 p.__proto__,也就是Programmer.prototype,也就是p1中去找,由于p1中也没有Say,那就去 p.__proto__.__proto__,也就是Person.prototype中去找,于是就找到了Say方法。这也就是原型链的实现原理。

  以下代码展示了JS引擎如何查找属性:

JavaScript学习总结(十七)——Javascript原型链的原理
1 function getProperty(obj, prop) {
2 if (obj.hasOwnProperty(prop))
3 return obj[prop];
4 else if (obj.__proto__ !== null)
5 return getProperty(obj.__proto__, prop);//递归
6 else
7 return undefined;
8 }
JavaScript学习总结(十七)——Javascript原型链的原理

  范例:查找p对象的Say方法

JavaScript学习总结(十七)——Javascript原型链的原理
 1  <script type="text/javascript">
2 /*查找obj对象的prop属性*/
3 function getProperty(obj, prop) {
4 if (obj.hasOwnProperty(prop))
5 return obj[prop];
6 else if (obj.__proto__ !== null)
7 return getProperty(obj.__proto__, prop);//递归
8 else
9 return undefined;
10 }
11
12 var Person = function () { };//定义Person类
13 Person.prototype.Say = function () {
14 alert("Person say");
15 }
16 Person.prototype.Salary = 50000;
17
18 var Programmer = function () { };//定义Programmer类
19 //Programmer.prototype = new Person();//让程序员类从人这个类继承,写法一
20 Programmer.prototype = Person.prototype;//让程序员类从人这个类继承,写法二
21 Programmer.prototype.WriteCode = function () {
22 alert("programmer writes code");
23 };
24 Programmer.prototype.Salary = 500;
25 var p = new Programmer();
26 var SayFn = getProperty(p,"Say");//查找p对象的Say方法
27 SayFn.call(p);//调用找到的Say方法
28 </script>
JavaScript学习总结(十七)——Javascript原型链的原理

在火狐下的运行结果:

  aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAO8AAACSCAIAAAAirgrxAAAFGUlEQVR4nO3bz07bSADH8XmXvIYLtIeEa1Wplz6B+xKRCBdLQP760FUF6snn3EpV1KoupxYIW6AVla8r0d3VNtAeKIfsYdRZ1wHULoNCfv5+FEWTwWNb5itjIcWsNFutdjeO414vXlputru9OI7b3d7ScrPbi+M4brW7y81WHMdxHDdbnWarY8csZOFNW2hWV1cXFxdDYMotLi6aRqMxGAxGwJTb3t42YRhO+jQAP6gZOqgZOqgZOqgZOqgZOqgZOqgZOqgZOqgZOqgZOqgZOqgZOqgZOqgZOqgZOqgZOqgZOqgZOqgZOqgZOqgZOqgZOqgZOqgZOqgZOqgZOqgZOqgZOqgZOqgZOqgZOqgZOqgZOqgZOqgZOqgZOqgZOqj5B2EYmu8qlcqkTwe/hpqLXMRJkkRRNNmTwS+h5iJqnl7UXDT+pJEkiZ2xcdunkSiK3GA0GkVRVFjlZtyqSqVSq9WMMbVa7aKjj+/HPfzkDxSGoRtc58WYMlyOosLjcpZl7hJFUZRl2Wg0StPUGJMkidvM3cXtxmmaumTDMEzTdDQaGWPs4PKa8/vJsszNuFW1Ws2eBr+7AmouKtRsw3VsjmmaFh5C3GZ2gyiKXOvuicXt+ZK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ubRo98ajUYITLlGo2Ha3V6nG9uXHXd7/43z851u3On2Ot/HLGThTVv4L+4gsnLP97rlAAAAAElFTkSuQmCC" alt="" />

  其实prototype只是一个假象,他在实现原型链中只是起到了一个辅助作用,换句话说,他只是在new的时候有着一定的价值,而原型链的本质,其实在于__proto__。

上一篇:mybatis集成redis


下一篇:2018.4.17 VFS