JavaScript checks whether a function is a native code method. javascriptfunction

Source: Internet
Author: User

JavaScript checks whether a function is a native code method. 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 ;}());
isNative(isNative) //false isNative(alert) //true window.isNative(window.isNative) //false window.isNative(window.alert) //true window.isNative(String.toString) //true

In javascript, name a method in the function to control the jump. The code in the method is not written.

<Script type = "text/javascript">
Function zhuce ()
{
Document. location. href = "register. jsp ";
}
</Script>
<Input type = "submit" name = "Submit2" value = "register" onclick = "zhuce ()"/>

In javascript, functions can include multiple functions.

Functions can include functions, but the latter cannot be accessed by code outside the former, which is equivalent to internal methods. The cause of multiple confirmation in the pop-up warning box may be that you checked all the text boxes in the onblur and then popped out all the nonconformities (just guessing, without looking at the code and not knowing the specific situation ), the better way is to put the error column behind the text box to prevent the pop-up window from interrupting the user to fill out the table. In addition, onblur and submit do not affect each other.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.