JavaScript to determine whether a function is a native code

Source: Internet
Author: User

I always encounter situations where it is necessary to check whether a function is native--a very important part of the functional test: The function is supported by the browser, or is modeled by a Third-party class library. The easiest way to detect this is, of course, to judge the value returned by the ToString method of the function.

JavaScript code

It is quite simple to judge whether a function is a native method:

1//judge whether the native function

2function isnative (FN) {

3//Example:

4//alert.tostring ()

5//"function alert () {[native code]}"

6//' + FN utilizes the implicit type conversion of JS.

7return (/{s*[native code]s*}/). Test ("+ FN");

8}

The principle of implementation is to convert a function to a string representation and perform a regular match.

Upgraded version, update!

(function () {

02

03//gets the ToString method of object to process the internal (internal) ' [[Class]] ' of the passed-in parameter value

04var toString = Object.prototype.toString;

05

06//the ToString method that obtains the original function, which is used to process the functions code

07var fntostring = Function.prototype.toString;

08

09//is used to detect the Host object Builder (host constructors).

10//(Safari > 4; True output of a particular array, really typed array specific)

11var Rehostctor =/^[object. +? constructor]$/;

12

13//uses regexp to compile commonly used native methods into regular templates.

14//uses ' object#tostring ' because he's generally not contaminated.

15var renative = RegExp (' ^ ' +

16//' object#tostring ' strong into a string

17String (toString)

18//to escape special characters associated with all regular expressions

19.replace (/[.*+?^${} () |[] /]/g, ' $& ')

20//to preserve the versatility of the template, replace ' toString ' with '. *? '

21//will replace characters such as ' for ... ', compatible with rhino and other environments, because they will have additional information, such as the number of parameters for the method.

22.replace (/tostring| ( function). *? (? = () | for. +? (?=]) /g, ' $1.*? '

23//End Character

24+ ' $ '

25);

26

27function isnative (value) {

28//Judge TypeOf

29var type = typeof value;

30return type = = ' function '

31//uses the ' function#tostring ' native method to invoke the

32//rather than value's own ' toString ' method,

33//to avoid being deceived by forgery.

34? Renative.test (Fntostring.call (value))

35//if type is not ' function ',

36//will need to check the situation of the host object (host objects).

37//because some (browser) environments use typed arrays or something as a DOM method

38//may not match the standard native regular mode at this time

(value && type = = ' object ' && rehostctor.test (Tostring.call (value)) | | False

40};

41

42//can assign isnative to the variable/object you want

43window.isnative = isnative;

44} ());

Test code:

1isNative (isnative)//false

2isNative (Alert)//true

3window.isnative (window.isnative)//false

4window.isnative (Window.alert)//true

5window.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.