javascript对象冒充

来源:互联网 发布:python 抓取html 编辑:程序博客网 时间:2024/04/30 06:50

本文出自:http://bbs.blueidea.com/thread-2850905-1-1.html

 

function a(kkk){
  this.color=kkk;
  this.sayColor=function(){
    alert(this.color);
}
}


function b(kkk){
  this.newMethod=a;
  this.newMethod(kkk) //不理解这条语句,这个的意思不是b.newMethod(kkk)吗,为什么可以直接就b(kkk)?   .newMethod不是一个属性么
  delete this.newMethod; //为什么去掉这个属性还能b(kkk)
}

 

先分析这个:
       obj.onclick = function(){};       
       那么这个function中的this就指向当前对象obj
同理:
       this.newMethod(kkk);       
       这个newMethod(kkk)是一个函数,newMethod(kkk)函数内部的this,将指向this.newMethod(kkk)这个this
那么:
       var t = new b("aaa");       
       b内部的this就指向了t
       也就是this.newMethod(kkk)这个this是t
       那么newMethod(kkk)函数内部的this也会指向t(见上面分析)
       所以实现了属性和方法的复制

这是一种函数作用域机制,特殊的地方在于构造函数本身是一个函数,这就导致了this的动态变化

 

 this.sayColor=function(){
    alert(this.color);
    }
}
function b(kkk){
  this.newMethod= a;
  this.newMethod(kkk);
  alert(this.color);
  alert(this.newMethod.color);
  delete this.newMethod;
}
var x = new b("fd");
</script>

 

 

谢谢,草履虫的BLOG-记录生活,学习的点点滴滴 我看过你的BLOG,这种详细的分解就是爽
www.withoume.com

换个写法来理解

a=function (kkk){
  this.color=kkk;
  this.sayColor=function(){
    alert(this.color);
}
}

function b(kkk){
  this.newMethod=a;
  this.newMethod(kkk); //这句是必须写的吗,上句不是已经把函数导入了吗?
  delete.this.newMethod; //这句且且是为了防止新方法会覆盖超类的属性和方法?
}

a=function (kkk){
  this.color=kkk;
  this.sayColor=function(){
    alert(this.color);
}
}

function b(kkk){
  this.newMethod=a(kkk); //这样写行吗,我试过了,可行,但是不懂原理啊
  delete.this.newMethod;
}

unction b(kkk){
  this.newMethod=a(kkk); //这样写行吗,我试过了,可行,但是不懂原理啊
  delete.this.newMethod;
}

————————————————
上面这么写好像不对吧。
this.newMethod=a(kkk);
你这样a函数就执行了,返回值是undefined, 也是就是this.newMethod的返回值是undefined


this.newMethod=a;
this.newMethod(kkk);
这两句相当于:
function   b(kkk){
  //复制:this.newMethod = a的效果相当于:
  //要明白这里的this是b的实例对象
  this.newMethod   =   function(kkk){
        this.color   =   kkk;
        this.sayColor   =   function(){
            alert(this.color);
        };
    };
    //执行:b的实例被赋值(方法和属性),他们的this是共同的
    this.newMethod(kkk);
    //删除:a这个类中的this被this.newMethod引用后也指向b的实例,导致了b和a的关联,这句话删除两个类的关联
    //由于this.newMethod(kkk)被执行了,b的实例已经获取了a的属性和方法,删除的只是b的一个属性newMethod而已
    delete   this.newMethod;
}

---------------------------------------------------------
this.newMethod=a(kkk)这种写法的问题在于:
a(kkk)被执行了,它不是一个匿名函数,它的this指向的是a这个类本身,所以得到的只是a这个函数的返回值undefined
也就是 this.newMethod = undefined 再 delete this.newMethod,没有起到任何效果
twitter:caolvchong


<script>
function a(kkk){
  this.color=kkk;
  this.sayColor=function(){
    alert(this.color);
    }
  alert(this.color);
}
function b(kkk){
  this.color = "我是b的颜色";
  this.newMethod= a(kkk);
  alert(this.color);
  delete this.newMethod;
}
var x = new b("实例化的颜色");
alert(x.color);
</script>


<script>
function a(kkk){
  this.color=kkk;
  this.sayColor=function(){
    alert(this.color);
    }
  alert(this.color);
}
function b(kkk){
  this.color = "我是b的颜色";
  this.newMethod= a;
  this.newMethod(kkk);
  alert(this.color);
  delete this.newMethod;
}
谢谢,我已经理解得很多了,我知道我不理解的原因了。
我一直死扣function a 的数据 传输给 function b ,其实function b 里面那句是起到纽带的作