在JavaScript中apply使用这种写法
y.apply(y, {
getPage:function(){return ym_body.firstChild.tagName.toLowerCase()=='iframe'?ym_body.firstChild:null},
show:function(args,fargs){ //显示消息框,fargs:优先配置,会覆盖args中的配置
//支持两种参数传入方式:(1)JSON方式 (2)多个参数传入
var a=Array.prototype.slice.call(args,0),o={};
if(typeof a[0]!='object'){
var cfg=['message','width','height','title','handler','maskAlphaColor','maskAlpha','iframe','icoCls','btn','autoClose','fixPosition','dragOut','titleBar'];
for(var i=0,l=a.length;i<l;i++) if(a[i]) o[cfg[i]]=a[i];
}else{
o=a[0];
}
y.apply(curCfg,y.apply({},o,fargs),y.setDefaultCfg()); //先还原默认配置
init();
},
doHandler:function(sign,autoClose){
if(typeof autoClose=='undefined'?curCfg.autoClose:autoClose)destroy();
eval(curCfg.handler)(sign);
},
//设定默认配置
setDefaultCfg:function(cfg){
return y.cfg=y.apply({},cfg,y.apply({},y.cfg,dftCfg));
}
});
为什么可以执行成功
而这种写法
var data=function(){}
data.apply(data,{show:function(){alert("");}},{show1:function(){alert("");}});
var d=new data();
d.show();
或者是这种写法
var data=function(){}
var show=function(){
   alert("show");
}
data.apply(data,{show:show},{show1:function(){alert("");}});
var d=new data();
d.show();
就不能执行成功
请哪位js高手指点下

解决方案 »

  1.   

    funObj.apply([thisObj[,argArray]])应用某一对象的一个方法,用另一个对象替换当前对象。functionObj的方法执行时,函数中的this对象会被thisObj替换掉。thisObj 可选项。将被用作当前对象的对象。argArray 可选项。将被传递给该函数的参数数组。//apply在对象继承方面的应用,不使用prototype,隐式的将父对象属性赋给了子对象
    function par(name)
    {
      this.parname=name;
    }
    function child(chname,parname){
      this.chname=chname;
      par.apply(this,new Array(parname));
    };var o=new child("john","Mr john");
    alert(o.parname+";"+o.chname);
    //apply可以在通用的方法调用方面进行使用
    window.onunload=function()
    {
      alert("unload event is fired!");
    }function sayBye(name,toName)
    {
      alert(name+" says bye to "+toName);
    }
    function sayEndBiz(name,toName,content)
    {
      alert(name+" ends his talk about "+content +" with "+toName);
    }function addTo(args,func)
    {
      var oldHandler=window.onunload||function(){};
      window.onunload=function()
      {
       func.apply(window,args);
       oldHandler.apply(window, args);
      }
    }addTo(new Array("John","everyone"),sayBye);
    addTo(new Array("John","everyone","deveopment strategy of the company"),sayEndBiz) 
      

  2.   


    data.apply(data,{show:"sss"},{show1:111});
    看看,可能是你传递的参数类型不匹配吧
      

  3.   

    参数写成数组形式
    ----------------------------------
    而这种写法
    var data=function(){}
    data.apply(data,[{show:function(){alert("");}},{show1:function(){alert("");}}]);
    var d=new data();
    d.show();
    或者是这种写法
    var data=function(){}
    var show=function(){
      alert("show");
    }
    data.apply(data,[{show:show},{show1:function(){alert("");}}]);
    var d=new data();
    d.show();
      

  4.   

    我在没有提问的时候试过三楼的写法写了以后d.show()找不到方法
      

  5.   

    一楼的
    http://hi.baidu.com/shashadu/blog/item/bc702181573738d4bd3e1e7d.html
      

  6.   

    整体思路上有些偏差,在这里我简单讲一下
    Function.apply(thisObj,[参数1,参数2...])
    这个方法只能接受两个参数,第一个参数的作用是,在本次调用中替换目标函数中的this指向的对象;
    第二个参数必须是一个数组,里面是函数的参数列表;
    举个例子://首先定义一个函数
    var data = function(a,b,c){ this.name = a+b+c;
    }
    //自定义一个this对象
    var thisObj = {name:'tom'};
    data.apply(thisObj,[1,2,3]);alert(thisObj.name);//显示6,因为在data函数中被修改了//有了上面的思想就很轻松的做出你要的效果了,但是要注意一点光是apply了不会把传入的对象附加到原型中,必须手动赋值this.prototype=proto;
    var data=function(proto){
    this.prototype=proto;
    }
    data.apply(data,[
    {
    show:function(){alert("");},
    show1:function(){alert("");}
    }
    ]);var d=new data();
    d.show();//显示空字符