论坛首页 Web前端技术论坛

prototype中bind的一个小问题

浏览 5680 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2005-12-26  
function testBind();{
      	var a=new Object();;
      	a.p='a';
      	a.foo=function();{
      		return this.p;
      	};      	
      	assertEquals(a.p, a.foo(););;  
      	      	
      	var b=new Object();;
      	b.p='b';      	         	
      	var f=a.foo.bind(b);;
                alert(typeof f+'\n'+f);;
                assertEquals(b.p, f(););;
}

结果发现f()是undefined,
但alert(typeof f+'\n'+f);显示f确实是个function呀
昏特了
(上面代码中用了jsUnit)
   发表时间:2005-12-27  
prototype的bind有些设计不妥,它没有返回值,你在bind()的源码中加入一个return就可以了。
Function.prototype.bind = function(object); {
  var __method = this;
  return function(); {
    return __method.apply(object, arguments);;;
  }
}
0 请登录后投票
   发表时间:2005-12-27  
有道理,谢谢了,回头试试
0 请登录后投票
   发表时间:2005-12-28  
匿名人士818745 写道
有道理,谢谢了,回头试试


prototype 1.4 已经修正了这个bug,而且 bind 方法还支持传递其他的参数了
Function.prototype.bind = function(); {
  var __method = this, args = $A(arguments);, object = args.shift();;
  return function(); {
    return __method.apply(object, args.concat($A(arguments);););;
  }
}
0 请登录后投票
   发表时间:2005-12-28  
晕了,prototype主页上是1.3.1版本,http://dev.conio.net/repos/prototype/dist/下的才是1.4
0 请登录后投票
论坛首页 Web前端技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics