15
15
// stuff
16
16
} ;
17
17
18
+ // better
18
19
if ( ! Array . prototype . map ) {
19
20
Array . prototype . map = function ( ) {
20
21
// stuff
21
22
} ;
22
23
}
23
24
25
+ // even better
24
26
if ( typeof Array . prototype . map !== "function" ) {
25
27
Array . prototype . map = function ( ) {
26
28
// stuff
27
29
}
28
30
}
29
31
30
- // browsers seem to be a little frivolous with white spaces and new lines
32
+
33
+ // If use either native or your own implementation, but not others:
34
+ // When you call toString() of a native function it should return a string with a function that has a body of [native code]
35
+
36
+ // by default there is white spaces and new lines issue
31
37
console . log ( Array . prototype . map . toString ( ) . replace ( / \s / g, '*' ) ) ;
32
38
// "*function*map()*{*****[native*code]*}*" // IE
33
39
// "function*map()*{*****[native*code]*}" // FF
34
40
// "function*map()*{*[native*code]*}" // Chrome
35
41
42
+ // a proper check can fix the problem
36
43
console . log ( Array . prototype . map . toString ( ) . replace ( / \s / g, '' ) ) ;
37
44
// "functionmap(){[nativecode]}"
38
45
46
+ // a reusable shim() function
39
47
function shim ( o , prop , fn ) {
40
48
var nbody = "function" + prop + "(){nativecode]}" ;
41
49
if ( o . hasOwnProperty ( prop ) &&
62
70
[ 1 , 2 , 3 ] . mapzer ( ) ; // alerts 1,2,3
63
71
64
72
// References
65
- // http://net.tutsplus .com/tutorials/javascript-ajax/the-essentials-of-writing-high-quality-javascript /
73
+ // http://www.jspatterns .com/shim-sniffing /
66
74
</ script >
67
75
</ body >
68
76
</ html >
0 commit comments