Checks whether a function is a JavaScript native function.

Source: Internet
Author: User
In my development work, I often encounter the need to determine whether a function is a JavaScript native function, sometimes this is a very necessary job. In my development work, I often encounter the need to determine whether a function is a JavaScript native function. Sometimes this is a very necessary job, you need to know whether the function is provided by the browser or encapsulated by a third party and disguised as a native function. Of course, the best way is to evaluate the return value of the toString method that executes this function.

The JavaScript

The method for completing this task is very simple:

function isNative(fn) {    return (/\{\s*\[native code\]\s*\}/).test('' + fn);}

The toString method returns the string form of this method, and then uses a regular expression to determine the characters contained in it.

More Powerful Method

John David Dalton, founder of Lodash, found a better solution:

;(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's 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 will represent      // things like typed arrays as DOM methods which may not conform to the      // normal native pattern.      : (value && type == 'object' && reHostCtor.test(toString.call(value))) || false;  }     // export however you want  module.exports = isNative;}());

Now you can see that it is complicated, but more powerful. Of course, this is not for security protection. It just provides you with 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.