Skip to content

Commit 27209e8

Browse files
committed
added shim sniffing to general patterns
1 parent d24cefd commit 27209e8

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

general-patterns/shim-sniffing.html

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,27 +15,35 @@
1515
// stuff
1616
};
1717

18+
// better
1819
if (!Array.prototype.map) {
1920
Array.prototype.map = function() {
2021
// stuff
2122
};
2223
}
2324

25+
// even better
2426
if (typeof Array.prototype.map !== "function") {
2527
Array.prototype.map = function() {
2628
// stuff
2729
}
2830
}
2931

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
3137
console.log(Array.prototype.map.toString().replace(/\s/g, '*'));
3238
// "*function*map()*{*****[native*code]*}*" // IE
3339
// "function*map()*{*****[native*code]*}" // FF
3440
// "function*map()*{*[native*code]*}" // Chrome
3541

42+
// a proper check can fix the problem
3643
console.log(Array.prototype.map.toString().replace(/\s/g, ''));
3744
// "functionmap(){[nativecode]}"
3845

46+
// a reusable shim() function
3947
function shim(o, prop, fn) {
4048
var nbody = "function" + prop + "(){nativecode]}";
4149
if (o.hasOwnProperty(prop) &&
@@ -62,7 +70,7 @@
6270
[1,2,3].mapzer(); // alerts 1,2,3
6371

6472
// References
65-
// http://net.tutsplus.com/tutorials/javascript-ajax/the-essentials-of-writing-high-quality-javascript/
73+
// http://www.jspatterns.com/shim-sniffing/
6674
</script>
6775
</body>
6876
</html>

0 commit comments

Comments
 (0)