Two examples of determining native functions in JavaScript

Source: Internet
Author: User

Two examples of determining native functions in JavaScript
Original article: Detect if a Function is Native Code with JavaScript
Original Article Date:
Translation Date:
Translated by: Tie

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 judge the function.ToStringThe value returned by the method.

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!

John-David Dalton, author of Lodash, shared a better solution:
; (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)                  //falseisNative(alert)                        //truewindow.isNative(window.isNative) //falsewindow.isNative(window.alert)       //truewindow.isNative(String.toString)    //true


Related Article

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.