This doesn't work:
这不起作用:
var s = '^foo';
console.log(['boot', 'foot'].some(s.match));
Uncaught TypeError: String.prototype.match called on null or undefined
But this does:
但这样做:
var s = '^foo';
console.log(['boot', 'foot'].some(function(i) { return i.match(s) }));
Why is this? I imagine somehow the String.prototype.match
function is too "primitive" or something, but why exactly? Since I'm not using ES2015, the second version seems quite verbose. Is there an alternative?
为什么是这样?我想象String.prototype.match函数太过“原始”或者某种东西,但为什么呢?由于我没有使用ES2015,第二个版本似乎相当冗长。还有其他选择吗?
EDIT
When I wrote the above, I actually got it backwards compared to my actual need, which was matching one string against a number of regexes. But thanks to the great answers and comments below, I get it: [/^foo/, /^boo/].some(''.match, 'boot')
.
当我写上面的内容时,实际上我的实际需要与实际需要相比,它实际上需要将一个字符串与一些正则表达式相匹配。但是由于下面的好答案和评论,我得到它:[/ ^ foo /,/^ boo /] .some(''。match,'boot')。
2 个解决方案
#1
4
A function value in Javascript does not bring its object along with it. The value of s.match
is a plain function value, with no knowledge that you happened to find it attached to s
. In fact, no matter what String you access it through, it's always the same function value:
Javascript中的函数值不会带来它的对象。 s.match的值是一个普通的函数值,不知道你碰巧发现它附加到s。事实上,无论你通过什么String访问它,它总是相同的函数值:
"foo".match === "bar".match
//= true
When you call a function through an object, Javascript sets this
to that object for the duration of the function call. But as soon as anything comes between retrieving the function value and calling it, any object association is lost.
当您通过对象调用函数时,Javascript会在函数调用期间将此设置为该对象。但是只要在检索函数值和调用它之间出现任何问题,任何对象关联都将丢失。
You can create a function that does remember a specific this
value using bind
, as in @Felix King's answer. someFunction.bind(someObject)
has approximately the same meaning as function(arg1, arg2,...) { return someObject.someFunction(arg1, arg2,...); }
, but it automatically handles the number of parameters properly.
您可以创建一个使用bind记住特定值的函数,如@Felix King的答案。 someFunction.bind(someObject)与函数(arg1,arg2,...)具有大致相同的含义{return someObject.someFunction(arg1,arg2,...); },但它会自动正确处理参数的数量。
#2
6
Note: The value of this
is determined by how the function is called! (exception: bound and arrow functions)
注意:this的值取决于函数的调用方式! (例外:绑定和箭头功能)
If you pass s.match
to .some
, then the function will be called with this
set to the global object (e.g. window
) not the string it "belongs" to.
如果将s.match传递给.some,则该函数将使用此set调用全局对象(例如窗口)而不是它“所属”的字符串。
I.e. it would be equivalent to this:
即它等同于:
String.prototype.match.call(window, 'foo')
This cannot work because this
has to refer to a string object.
这不起作用,因为这必须引用一个字符串对象。
You could solve this by binding the function to a specific this
value:
您可以通过将函数绑定到特定的此值来解决此问题:
['boot', 'foot'].some(s.match.bind(s));
Learn more about this
:
了解更多相关信息:
- MDN -
this
- You Don't Know JS:
this
or That? - How to access the correct `this` context inside a callback?
MDN - 这个
你不懂JS:这个还是那个?
如何在回调中访问正确的`this`上下文?
#1
4
A function value in Javascript does not bring its object along with it. The value of s.match
is a plain function value, with no knowledge that you happened to find it attached to s
. In fact, no matter what String you access it through, it's always the same function value:
Javascript中的函数值不会带来它的对象。 s.match的值是一个普通的函数值,不知道你碰巧发现它附加到s。事实上,无论你通过什么String访问它,它总是相同的函数值:
"foo".match === "bar".match
//= true
When you call a function through an object, Javascript sets this
to that object for the duration of the function call. But as soon as anything comes between retrieving the function value and calling it, any object association is lost.
当您通过对象调用函数时,Javascript会在函数调用期间将此设置为该对象。但是只要在检索函数值和调用它之间出现任何问题,任何对象关联都将丢失。
You can create a function that does remember a specific this
value using bind
, as in @Felix King's answer. someFunction.bind(someObject)
has approximately the same meaning as function(arg1, arg2,...) { return someObject.someFunction(arg1, arg2,...); }
, but it automatically handles the number of parameters properly.
您可以创建一个使用bind记住特定值的函数,如@Felix King的答案。 someFunction.bind(someObject)与函数(arg1,arg2,...)具有大致相同的含义{return someObject.someFunction(arg1,arg2,...); },但它会自动正确处理参数的数量。
#2
6
Note: The value of this
is determined by how the function is called! (exception: bound and arrow functions)
注意:this的值取决于函数的调用方式! (例外:绑定和箭头功能)
If you pass s.match
to .some
, then the function will be called with this
set to the global object (e.g. window
) not the string it "belongs" to.
如果将s.match传递给.some,则该函数将使用此set调用全局对象(例如窗口)而不是它“所属”的字符串。
I.e. it would be equivalent to this:
即它等同于:
String.prototype.match.call(window, 'foo')
This cannot work because this
has to refer to a string object.
这不起作用,因为这必须引用一个字符串对象。
You could solve this by binding the function to a specific this
value:
您可以通过将函数绑定到特定的此值来解决此问题:
['boot', 'foot'].some(s.match.bind(s));
Learn more about this
:
了解更多相关信息:
- MDN -
this
- You Don't Know JS:
this
or That? - How to access the correct `this` context inside a callback?
MDN - 这个
你不懂JS:这个还是那个?
如何在回调中访问正确的`this`上下文?