码迷,mamicode.com
首页 > 其他好文 > 详细

运行程序,解读this指向---case6

时间:2017-09-09 18:55:54      阅读:159      评论:0      收藏:0      [点我收藏+]

标签:cti   ase   function   color   col   child   var   prototype   log   

function Parent() {
  this.a = 1;
  this.b = [1, 2, this.a];
  this.c = { ckey: 5 };
  this.show = function () {
    console.log(this.a , this.b , this.c.ckey);
  }
}
function Child() {
  this.a = 2;
  this.change = function () {
    this.b.push(this.a);
    this.a = this.b.length;
    this.c.ckey = this.a++;
  }
}
Child.prototype = new Parent(); 
var parent = new Parent();
var child1 = new Child();
var child2 = new Child();
child1.a = 11;
child2.a = 12;
parent.show(); // 1 [1,2,1] 5
child1.show(); // 11 [1,2,1] 5
child2.show(); // 12 [1,2,1] 5
child1.change();
child2.change(); 
parent.show(); // 1 [1,2,1] 5
child1.show(); // 5 [1,2,1,11,12] 5
child2.show(); // 6 [1,2,1,11,12] 5

 

运行程序,解读this指向---case6

标签:cti   ase   function   color   col   child   var   prototype   log   

原文地址:http://www.cnblogs.com/camille666/p/this_case6.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!