{
assert.notNull(pObject, "not object to bind function");
var __method = this;
var result = function methodBinder()
{
var methodRet = __method.apply(pObject, arguments);
return methodRet;
};
result._bindedFunc = this;
return(result);
};
为Function原型对象添加bind方法,这个方法是在下面这个情况下使用
function JSClass()
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
值得注意的是:
1、在attachEvent后,触发事件执行的时候 alert(this.m_Text);这个this是在Event发生的时候,指向的是Window对象(应该是JS里面这样规定好的,在js装载的时候,就已经为所有的变量(除了this)分配好了作用域,this在不同的执行环境所指代的都不同)
2、除非我们在触发时间执行的时候能够找到这个方法真正所属于的那个对象,再把this替换过来就可以了;
3、在Function.prototype.bind 中,关键点是pObject这个传入的参数,在js加载的时候,应该是已经把apply(pObject, arguments);中的pObject建立了作用域,就是把pObject指向在bind的时候传入的那个对象,所以在真正触发调用的时候,解释执行var methodRet = __method.apply(pObject, arguments);
的时候可以找到pObject是当时bind那个对象;
4、其实看上去就是利用闭包函数奇怪的作用域这个特点: