Translation: Code sniffing in Javascript

Source: Internet
Author: User

Note: What is inappropriate in the Translation? Thank you for your correction and wish you a happy double festival!

Link: http://www.jspatterns.com/shim-sniffing/

It is not good to extend native objects and prototype without special needs.

 
1 //Do not do this2Array. Prototype. Map =Function(){3//SomeCode4};

Unless this is worthwhile, for example, adding someMethods In ecmascript5.

In this case, we generally do this:

1 If(!Array. Prototype. Map ){2Array. Prototype. Map =Function(){3// Some code4 };5}

If we are paranoidMapDefined as other unexpected values, suchTrueOr others, we can change the detection code to the following:

1 If(TypeofArray. Prototype. map! = "Function"){2Array. Prototype. Map =Function(){3//Some code4 };5}

(Although this will destroyMapDefine and affect the implementation of their functions)

However, in a hostile and brutal environment (in other words, but when you provide or use a JS Library), you should not trust anyone. If other people's JS Code is loaded before your JS Code, and in some way defines an es5 not fully compatibleMap ()Method, so that your code cannot run normally, what should I do?

However, you can trust the browser. If the WebKit kernel implements the map () method, you can rest assured that this method will certainly run normally. Otherwise, you will use your code for detection.

Fortunately, this is easy to implement in Javascript. When you call the tostring method of the native function, a function string is returned. The function body is[Native code].

For example, in the chrome console:

 
>Array. Prototype. Map. tostring ();"Function Map () {[native code]}"

A proper code check is always a little unpleasant, because different browsers are too rash in handling spaces and line breaks. The test is as follows:

1Array. Prototype. Map. tostring (). Replace (/\ s/g ,'*');2 //"* Function * map () * {****** [Native * Code] **} *" // IE3 //"Function * map () * {****** [Native * Code] *}" // FF4 //"Function * map () * {* [Native * Code] *}" // chrome

Just remove\ SYou will get a more practical string:

1Array. Prototype. Map. tostring (). Replace (/\ s/g ,'');2 //"Functionmap () {[nativecode]}"

You can encapsulate it into a reusable Shim () function, so you do not have to repeat all the similar! Array. prototype. This function accepts an object as a parameter (for example,Array. Prototype), An attribute to be added (for example'Map') And a function to be added.

 1   Function  Shim (O, prop, FN ){  2     VaR Nbody = "function" + Prop + "() {[nativecode]}" ;  3     If (O. hasownproperty (PROP )&& 4 O [prop]. tostring (). Replace (/\ s/g, '') = Nbody ){  5       //  The table name is native!  6       Return   True  ;  7   }  8     //  Newly Added  9 O [prop] =FN;  10 }

Test:

//This is a native method.Shim (array. prototype,'Map',Function(){/*...*/});//True//This is the newly added method.Shim (array. prototype,'Mapzer',Function() {Alert (This)});[1, 2, 3]. mapzer ();//Alerts 1, 2, 3

(End) ^_^

 

Related Article

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.