Augusto Moura (2017-07-05T20:15:39.000Z)
augusto.borgesm at gmail.com (2017-07-05T20:27:53.431Z)
You can wrap into a util function like that: ```js function customIndexOf() { return 'quack' } function withCustomIndexOf(fun) { const oldIndexOf = Array.prototype.indexOf Array.prototype.indexOf = customIndexOf fun() Array.prototype.indexOf = oldIndexOf } withCustomIndexOf(() => { console.log([123].indexOf()) // quack }) console.log([123].indexOf()) // -1 ``` Note, it will only work with sync code, async code (callbacks, promises) will ~probably~ be called after the reset of the prototype function **Edit:** The problem with this solution is because it will actually change behavior down to the call stack, third party functions will have the behavior compromised ```js withCustomIndexOf(() => { $('*').find('div') // may not work because will execute with the `customIndexOf` in Array.prototype instead of the default method }) ```
augusto.borgesm at gmail.com (2017-07-05T20:27:22.091Z)
You can wrap into a util function like that: ```js function customIndexOf() { return 'quack' } function withCustomIndexOf(fun) { const oldIndexOf = Array.prototype.indexOf Array.prototype.indexOf = customIndexOf fun() Array.prototype.indexOf = oldIndexOf } withCustomIndexOf(() => { console.log([123].indexOf()) // quack }) console.log([123].indexOf()) // -1 ``` Note, it will only work with sync code, async code (callbacks, promises) will ~probably~ be called after the reset of the prototype function **Edit:** The problem with this solution is because it will actually change behavior down to the call stack, third party functions will have the behavior compromised ```js withCustomIndexOf(() => { $('*').find('div') // may not work because it's using the `customIndexOf` instead of the default method }) ```
augusto.borgesm at gmail.com (2017-07-05T20:27:12.287Z)
You can wrap into a util function like that: ```js function customIndexOf() { return 'quack' } function withCustomIndexOf(fun) { const oldIndexOf = Array.prototype.indexOf Array.prototype.indexOf = customIndexOf fun() Array.prototype.indexOf = oldIndexOf } withCustomIndexOf(() => { console.log([123].indexOf()) // quack }) console.log([123].indexOf()) // -1 ``` Note, it will only work with sync code, async code (callbacks, promises) will ~probably~ be called after the reset of the prototype function **Edit:** The problem with this solution is because it will actually change behavior down to the call stack, third party functions will have the behavior compromised```js withCustomIndexOf(() => { $('*').find('div') // may not work because it's using the `customIndexOf` instead of the default method }) ```
augusto.borgesm at gmail.com (2017-07-05T20:26:27.804Z)
You can wrap into a util function like that: ```js function customIndexOf() { return 'quack' } function withCustomIndexOf(fun) { const oldIndexOf = Array.prototype.indexOf Array.prototype.indexOf = customIndexOf fun() Array.prototype.indexOf = oldIndexOf } withCustomIndexOf(() => { console.log([123].indexOf()) // quack }) console.log([123].indexOf()) // -1 ``` Note, it will only work with sync code, async code (callbacks, promises) will ~probably~ be called after the reset of the prototype function Edit: The problem with this solution is because it will actually change behavior down to the call stack, third party functions will have the behavior compromised ```js withCustomIndexOf(() => { $('*').find('div') // may not work because it's using the `customIndexOf` instead of the default method }) ```
augusto.borgesm at gmail.com (2017-07-05T20:18:22.329Z)
You can wrap into a util function like that: ```js function customIndexOf() { return 'quack' } function withCustomIndexOf(fun) { const oldIndexOf = Array.prototype.indexOf Array.prototype.indexOf = customIndexOf fun() Array.prototype.indexOf = oldIndexOf } withCustomIndexOf(() => { console.log([123].indexOf()) // quack }) console.log([123].indexOf()) // -1 ``` Note, it will only work with sync code, async code (callbacks, promises) will ~probably~ be called after the reset of the prototype function
augusto.borgesm at gmail.com (2017-07-05T20:18:06.443Z)
You can wrap into a util function like that: ``` .js function customIndexOf() { return 'quack' } function withCustomIndexOf(fun) { const oldIndexOf = Array.prototype.indexOf Array.prototype.indexOf = customIndexOf fun() Array.prototype.indexOf = oldIndexOf } withCustomIndexOf(() => { console.log([123].indexOf()) // quack }) console.log([123].indexOf()) // -1 ``` Note, it will only work with sync code, async code (callbacks, promises) will ~probably~ be called after the reset of the prototype function
augusto.borgesm at gmail.com (2017-07-05T20:17:58.861Z)
You can wrap into a util function like that: ``` .lang-javascript function customIndexOf() { return 'quack' } function withCustomIndexOf(fun) { const oldIndexOf = Array.prototype.indexOf Array.prototype.indexOf = customIndexOf fun() Array.prototype.indexOf = oldIndexOf } withCustomIndexOf(() => { console.log([123].indexOf()) // quack }) console.log([123].indexOf()) // -1 ``` Note, it will only work with sync code, async code (callbacks, promises) will ~probably~ be called after the reset of the prototype function