JS detects whether a function is a JavaScript native function

Source: Internet
Author: User
Tags regular expression

In my development work, I often encounter situations where it is necessary to determine whether a function is a JavaScript native function, and it is sometimes a necessity to know if the function is provided by the browser itself or by a third party, disguised as a native function. Of course, the best way to do this is to examine the return value of the ToString method that executes this function.

The JavaScript

The way to accomplish this task is simple:

The code is as follows Copy Code
function Isnative (FN) {
Return (/\{\s*\[native code\]\s*\}/). Test (' + FN ');
}

The ToString method returns the string form of the method and then uses the regular expression to determine which characters are contained within it.

More powerful way.

Lodash's founder, John-david Dalton, found a better solution:

The code is as follows Copy Code
;(function () {

Used to resolve the internal ' [[Class]] ' of values
var toString = Object.prototype.toString;

Used to resolve the decompiled source of functions
var fntostring = Function.prototype.toString;

Used to detect host constructors (Safari > 4; really typed array specific)
var rehostctor =/^\[object. +? constructor\]$/;

Compile a regexp using a common native method as a template.
We chose ' object#tostring ' because there ' a good chance it is not being mucked with.
var renative = RegExp (' ^ ' +
Coerce ' object#tostring ' to a string
String (toString)
Escape any special regexp characters
. Replace (/[.*+?^${} () |[ \]\/\\]/g, ' \\$& ')
Replace mentions of ' toString ' with '. ~ ' to keep the template generic.
Replace thing like ' for ... ' to support environments like Rhino which add extra info
such as Method arity.
. Replace (/tostring| ( function). *? (? =\\\ () | for. +? (? =\\\]) /g, ' $1.*? ' + '$'
);

function Isnative (value) {
var type = typeof value;
return type = = ' function '
Use ' function#tostring ' to bypass the value ' s own ' toString ' method
And avoid being faked out.
? Renative.test (Fntostring.call (value))
Fallback to a host object check because some environments'll represent
Things like typed arrays as DOM methods which may isn't conform to the
Normal native pattern.
: (value && type = = ' object ' && rehostctor.test (Tostring.call (value)) | | False
}

Export however you want
Module.exports = isnative;
}());

Now you see it, it's complicated, but it's more powerful. Of course, this is not for security protection, it just gives you information about whether it is a native function.

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.