什么对象的javascript函数被绑定(它的“this”是什么)? [英] What object javascript function is bound to (what is its "this")?

查看:102
本文介绍了什么对象的javascript函数被绑定(它的“this”是什么)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这个函数里面是这个

  var func = function {
return this.f === arguments.callee;
// =>如果绑定到某个对象,则为true
// => false,如果绑定为null,因为this.f === undefined
}

var f = func; //不受任何约束;

var obj = {};
obj1.f = func; //如果调用为obj1.f(),则绑定到obj1,但如果调用为func(),则不绑定如果调用为obj2,则绑定到obj2。 f()或绑定()



编辑:



因为 f 不会成为 obj2.f() obj2



编辑结束。



:如何找到对象,表示函数绑定到这个函数之外?

我想实现这个:

 函数g(f){
if(typeof(f)!=='function')throw'error:f should be function';

if(f.boundto()=== obj)
//如果g(obj1.f)被调用
doSomething(f);

... ....

if(f.boundto()=== obj2)
//如果g(obj2.f )或g(绑定)被称为
doSomethingElse(f);

$ / code>

和部分应用程序不改变函数绑定的对象:

 函数partial(f){
return f.bind(f.boundto(),arguments.slice(1)) ;
}



共识:



你不能这样做。外卖店:小心使用 bind 这个



您可以执行部分​​申请:

  //这让我们可以将slice方法作为函数
//调用到一个类似数组的对象上。
var slice = Function.prototype.call.bind(Array.prototype.slice);

函数partial(f / *,... args * /){

if(typeof f!='function')
throw new TypeError('预期功能');

var args = slice(arguments,1);

返回函数(/ * ... moreArgs * /){
返回f.apply(this,args.concat(slice(arguments)));
};

}






什么对象是这个函数绑定到?



另外,对于问题的第一部分,有一个非常直接的解决方案。不知道这是否是您的选择,但您可以很容易地在JS中进行猴子补丁。猴子修补绑定是完全可能的。

  var _bind =功能。 prototype.apply.bind(Function.prototype.bind); 
Object.defineProperty(Function.prototype,'bind',{
value:function(obj){
var boundFunction = _bind(this,arguments);
boundFunction.boundObject = obj;
return boundFunction;
}
});

在任何其他脚本运行之前运行该脚本,并使用绑定,它会自动为该函数添加一个 boundObject 属性:

  function f(){} 
var o = {};
var g = f.bind(o);
g.boundObject === o; // true

(注意:假设您处于上述ES5环境中,事实上你正在使用 bind 。)


I know that inside the function it is this.

var func = function {
    return this.f === arguments.callee; 
    // => true, if bound to some object
    // => false, if is bound to null, because this.f === undefined
}

var f = func; // not bound to anything;

var obj = {};
obj1.f = func; // bound to obj1 if called as obj1.f(), but not bound if called as func()

var bound = f.bind(obj2) // bound to obj2 if called as obj2.f() or as bound()

Edited:

You can't actually call obj2.f() as f doesn't become a property of obj2

edit end.

The question is: how to find the object, that the function is bound to, outside of this function?

I want to achieve this:

function g(f) {
  if (typeof(f) !== 'function') throw 'error: f should be function';

  if (f.boundto() === obj)
    // this code will run if g(obj1.f) was called
    doSomething(f);

  // ....

  if (f.boundto() === obj2)
    // this code will run if g(obj2.f) or g(bound) was called
    doSomethingElse(f);
}

and partial application without changing the object that the function is bound to:

function partial(f) {
   return f.bind(f.boundto(), arguments.slice(1));
}

Consensus:

You can't do it. Takeaway: use bind and this with great care :)

解决方案

Partial Application

You can do partial application:

// This lets us call the slice method as a function
// on an array-like object.
var slice = Function.prototype.call.bind(Array.prototype.slice);

function partial(f/*, ...args */) {

    if (typeof f != 'function')
        throw new TypeError('Function expected');

    var args = slice(arguments, 1);

    return function(/* ...moreArgs */) {
        return f.apply(this, args.concat(slice(arguments)));
    };

}


What Object is this Function Bound To?

Additionally, there's a pretty straight-forward solution to the first part of your question. Not sure if this is an option for you, but you can pretty easily monkey-patch things in JS. Monkey-patching bind is totally possible.

var _bind = Function.prototype.apply.bind(Function.prototype.bind);
Object.defineProperty(Function.prototype, 'bind', {
    value: function(obj) {
        var boundFunction = _bind(this, arguments);
        boundFunction.boundObject = obj;
        return boundFunction;
    }
});

Just run that before any other scripts get run, and any script which uses bind, it will automatically add a boundObject property to the function:

function f() { }
var o = { };
var g = f.bind(o);
g.boundObject === o; // true

(Note: I'm assuming you're in an ES5 environment above due to the fact that you're using bind.)

这篇关于什么对象的javascript函数被绑定(它的“this”是什么)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆