Two examples of JavaScript that determine native functions

Source: Internet
Author: User

Original link: Detect If a Function is Native Code with JavaScript
Original Date: 2014-08-17
Translation Date: 2014-08-20
Translators: Anchor

I always run into situations where I need to check if a function is native code-a very important part of the functional test: whether the function is built-in to the browser or simulated by a third-party class library. The simplest way to detect this is, of course, 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 if the primary function isnative (FN) {    ////    alert.tostring ()//    "function alert () {[native code]}"    //' ' + FN takes advantage of the implicit type conversion of JS.    Return (/\{\s*\[native code\]\s*\}/). Test (' + fn);}

Converts a function to a string representation and performs a regular match, which is how the implementation works.

upgraded version, update!

Lodash's author John-david Dalton shared a better approach:
;(function () {//Gets the ToString method of Object that handles the internal (internal) ' [[Class]] ' var tostring = Object.prototype.toString of the incoming parameter value;    Gets the original Function of the ToString method, which is used to handle functions's anti-compile code var fntostring = Function.prototype.toString; Used to detect the Host object constructor (host constructors),//(Safari > 4; True output-specific array, really typed array specific) var rehostctor =/^\[obje Ct. +?  constructor\]$/;  Use regexp to compile common native methods into regular templates. Use ' object#tostring ' is because generally he will not be contaminated var renative = RegExp (' ^ ' +//Will ' object#tostring ' strong to String (toString)/ /escapes special characters related to all regular expressions. Replace (/[.*+?^${} () |[    \]\/\\]/g, ' \\$& ')//in order to maintain the versatility of the template, the ' toString ' is replaced with '. * '///replace ' for ... ' characters, compatible with environments such as rhino, as they will have additional information, such as the number of parameters for the method. . Replace (/tostring| ( function). *? (? =\\\ () | for. +? (?=\\\])    /g, ' $1.*? ')    Terminator + ' $ ');    function Isnative (value) {//judgment typeof var type = typeof value;      return type = = ' function '//use ' function#tostring ' native method to invoke,//rather than value own ' toString ' method,//To avoid being deceived by forgery. ? RenativE.test (Fntostring.call (value))//If type is not ' function ',//You need to check the host object's case,//because some (browser) environment will Typ Ed arrays things like DOM methods//This may not match the standard native regular mode: (value && type = = ' object ' && rehostctor.test (t Ostring.call (value))) | |  False    }; You can assign isnative to the variable/object you want window.isnative = isnative;} ());

Test Code:
Isnative (isnative)                  //falseisnative (Alert)                        //truewindow.isnative (window.isnative)//falsewindow.isnative (Window.alert)       Truewindow.isnative (string.tostring)    //true


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.