JavaScript checks whether the native function checks whether the function is a native code.
I often encounter a situation where I need to check whether a function is a native code. This is a very important part of the function test: whether the function is supported by the browser or simulated by a third-party class library. To detect this, the simplest way is to determine the value returned by the toString method of the function.
JavaScript code
Determining whether a function is a native method is actually quite simple:
1 // determine whether the native function is used
2 function isNative (fn ){
3 // example:
4 // alert. toString ()
5 // "function alert () {[native code]}"
6 // ''+ fn uses js implicit type conversion.
7 return (// {\ s * \ [native code \] \ s * \}/). test (''+ fn );
8}
Converts a function to a string representation and performs regular matching. This is the principle of implementation.
Upgrade, Update!
01; (function (){
02
03 // obtain the toString method of the Object, which is used to process the internal (internal) '[[Class]' of the input parameter value
04var toString = Object. prototype. toString;
05
06 // obtain the toString method of the original Function for processing the decompilation code of functions
07var fnToString = Function. prototype. toString;
08
09 // host constructors ),
10 // (Safari> 4; returns a specific array, really typed array specific)
11var reHostCtor =/^ \ [object. +? Constructor \] $ /;
12
13 // use RegExp to compile common native methods into regular templates.
14 // use 'object # tostring' because it is generally not contaminated.
15var reNative = RegExp ('^' +
16 // convert 'object # tostring' into a string
17 String (toString)
18 // escape all special characters related to regular expressions
19. replace (/[. * +? ^ $ {} () | [\] \/\]/G, '\ $ &')
20 // replace 'tostring' '.*? '
21 // replace characters such as 'for...' and be compatible with environments such as Rhino, because they have additional information, such as the number of parameters of the method.
22. replace (/toString | (function ).*? (? ==\\ () | For. +? (? =\\])/G, '$1 .*? ')
23 // Terminator
24 + '$'
25 );
26
27 function isNative (value ){
28 // judge typeof
29var type = typeof value;
30 return type = 'function'
31 // use the native method of 'function # tostring' to call the Function,
32 // instead of the value's own 'tostring' method,
33 // prevent spoofing.
34? ReNative. test (fnToString. call (value ))
35 // if the type is not 'function ',
36 // check the host object,
37 // some (browser) environments use things like typed arrays as DOM Methods
38 // The standard Native regular mode may not be matched at this time
39: (value & type = 'object' & reHostCtor. test (toString. call (value) | false;
40 };
41
42 // assign the isNative value to the desired Variable/Object
43window. isNative = isNative;
44 }());
Test code:
1 isNative (isNative) // false
2 isNative (alert) // true
3window. isNative (window. isNative) // false
4window. isNative (window. alert) // true
5window. isNative (String. toString) // true