JavaScript checks whether the native function checks whether the function is a native code. javascriptfunction
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:
// Determine whether the native function isNative (fn) {// example: // alert. toString () // "function alert () {[native code]}" // ''+ fn uses the implicit type conversion of js. return (/\ {\ s * \ [native code \] \ s *\}/). test (''+ fn );}
Converts a function to a string representation and performs regular matching. This is the principle of implementation.
Upgrade, Update!
; (Function () {// obtain the Object's toString method, used to process the internal (internal) '[[Class] 'var toString = Object. prototype. toString; // The toString method for obtaining the original Function. It is used to process the decompilation code var fnToString = Function of functions. prototype. toString; // used to detect host constructors, // (Safari> 4; returns a specific array, really typed array specific) var reHostCtor =/^ \ [object. +? Constructor \] $/; // use RegExp to compile common native methods into regular templates. // use 'object # tostring' because it is generally not contaminated var reNative = RegExp ('^' + // convert 'object # tostring' to a String (toString) // escape all special characters related to regular expressions. replace (/[. * +? ^ $ {} () | [\] \/\]/G, '\ $ &') // to ensure the versatility of the template, replace 'tostring' '. *? '// Set'... 'and other characters, compatible with environments such as Rhino, because they have additional information, such as the number of parameters of the method .. replace (/toString | (function ). *? (? ==\\ () | For. +? (? =\\])/G, '$1 .*? ') // Terminator +' $ '); function isNative (value) {// judge typeof var type = typeof value; return type = 'function' // use the 'function # tostring' native method to call the function. // instead of the value's own 'tostring' method, // avoid spoofing .? ReNative. test (fnToString. call (value) // if the type is not 'function', // you need to check the situation of the host object. // some (browsers) the environment treats things such as typed arrays as DOM Methods // This may not match the standard Native regular mode: (value & type = 'object' & reHostCtor. test (toString. call (value) | false ;}; // assign isNative to the expected variable/Object window. isNative = isNative ;}());
Test code:
isNative(isNative) //false isNative(alert) //true window.isNative(window.isNative) //false window.isNative(window.alert) //true window.isNative(String.toString) //true
Javascript checks whether a function is executed
I don't know what you mean.
So I will provide you with the idea: If you want to execute fn4 () outside, you can customize a variable, which must be defined outside fn4! When fn4 () is executed, modify this variable and check whether it has been modified in fn6. as the building said, there is no need to hide text fields. the advantage is that if fn4 () is not executed on the periphery, it can be executed in fn6.
This method is simplified as follows:
// Define an external variable fn = false first;
Fn4 ()
{
// Code segment;
Return true;
}
Fn = fn4 ();
......
Fn6 (){
// Code segment;
If fn
{
// Code segment is allowed;
}
Else
......
}
How does JAVASCRIPT determine which function is called by the event?
The first floor is correct. If there is no name, this event is not bound to a function.
Str. match (/\ w *(? = \(\);)/)