This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// In ECMA-262 5th edition all methods of Array.prototype are intentionally generic. It means if you pass different | |
// native object than array for the this value, the particular method will work in same way as for array instances. | |
// ECMA-262 does not define how those methods will work if you pass a host object for the this value. That behavior // is implementation dependent. | |
// For example: | |
var arr = []; | |
Array.prototype.push.call(arr, 1, 2); | |
arr[0]; //1 | |
arr[1]; //2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function func(a, f) { | |
return function (args) { | |
args.__proto__ = a; | |
f.call(this, args); | |
}; | |
}; | |
var f = func({foo : 10, bar : 20}, function (args) { | |
print(args.foo, args.bar); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function valueRange(start, end) { | |
return function (value) { | |
return Math.min(Math.max(value, start), end); | |
}; | |
} | |
//Get new value range [0, 20] | |
var f = valueRange(0, 20); | |
console.log(f(10)); //10 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
RegExp.prototype.matches = function* (str) { | |
let moreThanOnce = this.global || this.sticky; | |
let myLastIndex = 0; | |
do { | |
// preserve lastIndex of another .exec() calls on same regexp | |
let savedLastIndex = this.lastIndex; | |
// use own state for lastIndex to match our str | |
this.lastIndex = myLastIndex; | |
let match = this.exec(str); |