Possible Duplicate:
Question on this JavaScript Syntax (“What Does This Do?”)
in this article i found this:
/xyz/.test(function(){xyz;})
i looked at this and i coudn’t figure out how does the xyz passed to the invoker. so i did some similar test in the console:
/xyz/.test(function(){xya;}) > false
/xyz/.test(function(){xyz;}) > true
/xyz/.test(function(){'xya';}) > false
/xyz/.test(function(){'xyz';}) > true
/xyz/.test(function(){console.log('xya');}) > false
/xyz/.test(function(){console.log('xyz');}) > true
/xyz/.test(function(xya){}) > false
/xyz/.test(function(xyz){}) > true
/fuc/.test(function(){}) > false
/func/.test(function(){}) > true
it seems that the .test() function converts the argument to string and then does the test. so why /xyz/.test(function(){xyz;}) used rather than /xyz/.test('xyz')?
Its effectively testing that:
(function(){xyz;}).toString()returns recognizable javascript source code:
"(function(){xyz;})"as opposed to something funky that some implementations may return.
It uses
.testto convert the function to a string then verifies that an internal token (xyz) is visible in the result.