testReplaceMap.js (803B)
1 // String.replace on functions returning hashmap elements. 2 3 function first() { 4 var arr = {a: "hello", b: "there"}; 5 var s = 'a|b'; 6 return s.replace(/[a-z]/g, function(a) { return arr[a]; }, 'g'); 7 } 8 assertEq(first(), "hello|there"); 9 10 function second() { 11 var arr = {a: "hello", c: "there"}; 12 var s = 'a|b|c'; 13 return s.replace(/[a-z]/g, function(a) { return arr[a]; }, 'g'); 14 } 15 assertEq(second(), "hello|undefined|there"); 16 17 Object.defineProperty(Object.prototype, "b", {get: function() { return "what"; }}); 18 19 assertEq(second(), "hello|what|there"); 20 21 function third() { 22 var arr = {a: "hello", b: {toString: function() { arr = {}; return "three"; }}, c: "there"}; 23 var s = 'a|b|c'; 24 return s.replace(/[a-z]/g, function(a) { return arr[a]; }, 'g'); 25 } 26 assertEq(third(), "hello|three|undefined");