Javascript validation function

Possible duplicate:
A question on this JavaScript syntax ("What should I do?")

in this article I found this:

/xyz/.test(function(){xyz;}) 

I looked at this, and I could not understand how xyz conveyed the call. so I did some similar tests 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 a string and then performs a test. therefore why is /xyz/.test(function(){xyz;}) used instead of /xyz/.test('xyz') ?

+8
javascript
source share
2 answers

Its effective testing:

(function(){xyz;}).toString()

returns the recognized javascript source code:

"(function(){xyz;})"

not something scared that some implementations might come back.

It uses .test to convert the function to a string, and then checks that an internal token ( xyz ) is displayed inside the result.

+5
source share

I have not read the entire article, but it looks like this is some kind of browser / function sniffing.

John checks to see if the anonymous function(){xyz;} actually converted to the string "function(){xyz;}" . There are probably browsers that convert it to something else and that it checks.

However, it seems to me that this is a little voodoo ...

0
source share

All Articles