简体   繁体   中英

Javascript regexp test behavior with function

Can anybody explain me this behavior? I dont get it... variable a is not touched. Where can be useful?

a === undefined // not touched -> on call variable a got exception (this behavior I understand)
/a/.test(function(){a}) // => true !!?

Thanks

EDIT:

Thanks much for answers, but I currently have one sub-question.

http://ejohn.org/blog/simple-javascript-inheritance/#ig-sh-2

on this piece of code:

fnTest = /xyz/.test(function(){xyz;}) ? /\b_super\b/ : /.*/

why John use this?, instead simple

fnTest = /\b_super\b/ 

if I understood right /xyz/.test(function(){xyz;}) will be every time true, just like

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

Where you have:

a === undefined // not touched -> on call variable a got exception

this is testing if the value of a is undefined . It will return true if a has either been declared and not assigned a value (in which case evaluation its value will return undefined ), or it has been assigned a value of undefined . If a has not be declared or otherwise initialised, it will throw a reference error.

In all other cases, it will return false .

In:

/a/.test(function(){a}) // => true !!?

the expression /a/ is a regular expression initialiser (or literal ) that creates a new regular expression as if by:

new RegExp('a');

which matches an "a" character anywhere in a string.

The test method of the regular expression is then called, which will return true or false. The argument to test is meant to be a string, so if it isn't it is type converted to a string. That will return a string that represents the function (essentially the actual code of the function). So you effectively have:

var re = new RegExp('a');
var s = 'function(){a}';
re.test(s);

which will return true (because there is an "a" in the string).

Edit

It seems you were asking in relation to this:

var initializing = false,
          fnTest = /xyz/.test(function(){xyz;}) ? /\b_super\b/ : /.*/;

What that is doing is creating a test for function objects based on the result of testing a function object with the regular expression test method.

The argument to the test method is supposed to be a string, so according to ECMA-262, if it isn't, it's converted to a string. If the argument is an object, then its toString method will be called, and for a function that usually returns the code in the function body, but not always and not in all ECMAScript hosts.

For example, built–in functions many return something like:

function pow() {
  [native code]
}

So what the above is doing is saying "if the function returns the function body, assign the following to fnTest :"

new RegExp('\\b_super\\b');

(Note that the double backslashes are required for this form of regular expression) which will match a string with the word "_super" in it.

And if the function doesn't return the function body, it sets fnTest to:

new RegExp('.*');

which will match any string with zero or more characters.

If you are trying to access an undefined variable (not the property of an object), you will get a ReferenceError .

To avoid this, define your variable:

var a;

a === undefined; //true

Now as for the second example:

/a/ is a regular expression literal and is the same as new RegExp('a'); so it has nothing to do with the a variable.

As for the last part, .test(function(){a}) , the function has not been executed yet so the a variable (which is not defined) wasn't accessed, therefore no error.

Also, it returns true because the test function expects a string, not a function, so it does an implicit conversion of the function to it's string value.

Therefore it is like if you were doing: /a/.test('function () {a}');

The reason for those behavior is that:

  • a === undefined ,it shows variable a got exception, because a is not defined, obviously.

  • /a/.test(funcion(){a}) it returns true,because /a/ create a regexp variable ,

  • inside test , function(){a} create a function, but it hasn't been called , so it wouldn't got exception.

But I don't get the reason for return true . = =

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM