Home  >  Article  >  Web Front-end  >  Tips for detecting whether a function is a JavaScript native function_javascript tips

Tips for detecting whether a function is a JavaScript native function_javascript tips

WBOY
WBOYOriginal
2016-05-16 16:09:571059browse

In my development work, I often encounter situations where I need to determine whether a function is a JavaScript native function. Sometimes this is a very necessary task. You need to know whether the function is provided by the browser itself or by a third party. Three-party encapsulation and disguise as native functions. Of course, the best way is to examine the return value of the toString method that executes this function.

The JavaScript

The way to complete this task is very simple:

Copy code The code is as follows:

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

The toString method will return the string form of this method, and then use regular expressions to determine the characters contained in it.

A more powerful method

Lodash founder John-David Dalton found a better solution:

Copy code The code is as follows:

;(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 see it, it's complex, but more powerful. Of course, this is not for security purposes, it just provides you with information about whether it is a native function.
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn