调用时分配给变量或函数参数的对象方法失败 [英] Object methods assigned to variables or function arguments fail when invoked

查看:128
本文介绍了调用时分配给变量或函数参数的对象方法失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在正在学习javascript,对我来说好像是美丽的函数式语言,这是从PHP开始的奇妙之举,我应该早一点这样做。虽然,我无法弄清楚这一点:

  var v1 =(/[abc]/).test; 
v1('a');

表示在不兼容undefined 上调用的测试方法,我试图将该正则表达式的测试方法存储到变量中,并在稍后调用它。



但它适用于我自己的函数:

  function foo(){
return'I\'f foo';
}

var f = foo;
f(); //返回我是foo

它也应该用于方法,因为函数只是父类的方法无论如何,对吧?

最终,我尝试这样做的原因是能够写出如下所示的内容:

  var a = ['a','b','c']; 
a.every((/[abc]/).test);

检查每个数组成员与该正则表达式。



为什么这不起作用?它传递内置函数的限制吗?或者我只是做错了什么?



PS:如果你现在碾磨牙齿,消除一些不良行为,扼杀好的做法,我只是在玩。但我也想听听他们的消息。

解决方案


它适用于我自己的函数


您不在函数中使用 this 。考虑这个例子:

pre $ $ $ $ $ $ $ $ $ $ $ $
alert(this.foo);
}
};
var f = obj.bar;
f(); //会提醒`undefined`,而不是`42`




对方法也是如此,因为函数只是父对象的方法,对吧?

方法只是一个口语函数分配给对象的属性。功能是独立的价值。没有连接到函数分配给的对象。这怎么可能,因为一个函数可以分配给多个对象?


为什么这不起作用?


这个这个是指在一个函数内,在运行时时间即可。所以如果你把这个函数赋值给一个变量并在以后调用它,那么

  var v1 =(/[abc]/).test; 
v1('a');

这个函数内部会引用 window ,而不是正则表达式对象。



您可以使用 .bind <以显式地将这个绑定到一个特定的值:

  var a = ['a','b','c']; 
var pattern = / [abc] /;
a.every(pattern.test.bind(pattern));

请注意,自 .bind 返回a函数,与使用函数表达式相比,唯一的优势是编写起来更简短。








不,每个方法/函数都存在这个问题,因为这就是功能起作用。内置函数的好处在于它们通常会明确告诉你何时这个指的是错误类型的对象(通过抛出错误)。






详细了解这个

I'm learning javascript right now, seems like beautiful functional language to me, it is wonderful move from PHP, I should have done this earlier. Although, I cannot figure this one out:

var v1 = (/[abc]/).test;
v1('a');

says test method called on incompatible undefined, I'm trying to store the test method of that regex into variable and invoke it later.

but it works with my own functions:

function foo(){
    return 'I\'m foo';
}

var f = foo;
f(); // returns I'm foo

It should work on methods too, since functions are just methods of parent object anyway, right?

Ultimately, the reason I'm trying this is to be able to write something like this:

var a = ['a', 'b', 'c'];
a.every( (/[abc]/).test );

to check each array member against that regex.

Why doesn't this work? Is it limitation in passing built-in functions around? Or am I just doing something wrong?

PS: If you grind your teeth now and muffling something about bad practices, screw good practices, I'm just playing. But I'd like to hear about them too.

解决方案

it works with my own functions

You are not using this inside the function. Consider this example:

var obj = {
    foo: 42,
    bar: function() {
        alert(this.foo);
    }
};
var f = obj.bar;
f(); // will alert `undefined`, not `42`

It should work on methods too, since functions are just methods of parent object anyway, right?

"Method" is just a colloquial term for a function assigned to a property on object. And functions are standalone values. There is no connection to the object a function is assigned to. How would this even be possible, since a function could be assigned to multiple objects?

Why doesn't this work?

What this refers to inside a function is determined at run time. So if you assign the function to a variable and call it later

var v1 = (/[abc]/).test;
v1('a');

this inside the function will refer to window, not to the regular expression object.

What you can do is use .bind [MDN] to explicitly bind this to a specific value:

var a = ['a', 'b', 'c'];
var pattern = /[abc]/;
a.every(pattern.test.bind(pattern));

Note though that since .bind returns a function, the only advantage over using a function expression is that it is a tad shorter to write.


Is it limitation in passing built-in functions around?

No, the problem exists for every method/function because that's how functions work. The nice thing about built-in functions though is that they often explicitly tell you when this is referring to the wrong type of object (by throwing an error).


Learn more about this.

这篇关于调用时分配给变量或函数参数的对象方法失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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