This does not work:
var s = '^foo'; console.log(['boot', 'foot'].some(s.match)); Uncaught TypeError: String.prototype.match called on null or undefined
But it does:
var s = '^foo'; console.log(['boot', 'foot'].some(function(i) { return i.match(s) }));
Why is this? I imagine somehow the function is String.prototype.matchtoo "primitive" or something like that, but why exactly? Since I am not using ES2015, the second version looks rather verbose. Is there an alternative?
String.prototype.match
EDIT
When I wrote above, I really got it back compared to my actual need, which corresponded to one line for several regular expressions. But thanks to the great answers and comments below, I understand [/^foo/, /^boo/].some(''.match, 'boot').
[/^foo/, /^boo/].some(''.match, 'boot')
Javascript . s.match - , , s. , , , :
s.match
s
"foo".match === "bar".match //= true
, Javascript this . - , .
this
, this, bind, @Felix King. someFunction.bind(someObject) , function(arg1, arg2,...) { return someObject.someFunction(arg1, arg2,...); }, .
bind
someFunction.bind(someObject)
function(arg1, arg2,...) { return someObject.someFunction(arg1, arg2,...); }
: this , ! (: )
s.match .some, this, (, window), , .
.some
window
.. :
String.prototype.match.call(window, 'foo')
, this .
, this:
['boot', 'foot'].some(s.match.bind(s));
this:
Source: https://habr.com/ru/post/1615170/More articles:Как "выкинуть новый ZygoteInit.MethodAndArgsCaller" очистить стеки кадров? - javaiOS - Problem Creating Custom Callout MKAnnotation - iosRecover Beanstalk resilient environment without losing ElasticIP - amazon-web-servicesAdd a multi-line footnote to the Grob table using gridextra in R - rChecking ipython library version from terminal? - terminalСамая быстрая проверка, начинается ли строка со значения в списке? - performanceHow to use FS when MODULARIZE and EXPORT_NAME are used in emscripten - browserNullPointerException in CMUSphinx - javaStructured Data> Microdata & Json-LD> Entity Identifiers> Fragment Identifier - uriRDF 303 Redirection Profiles - redirectAll Articles