Last active
September 22, 2019 12:28
-
-
Save kamaal-/59e734210a89aff3baabd16f3963416b to your computer and use it in GitHub Desktop.
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
const isOBject = obj => { | |
const type = typeof obj; | |
return type === "function" || (type === "object" && !!obj); | |
}; | |
const getRandomKey = () => { | |
return ( | |
[...Array(10)].map(_ => ((Math.random() * 36) | 0).toString(36)).join`` || | |
{} | |
); | |
}; | |
const checkRandomKey = (key, obj) => | |
obj[key] === undefined ? key : checkRandomKey(getRandomKey(), obj); | |
if (!Function.prototype.fauxCall) { | |
Function.prototype.fauxCall = function(_context) { | |
const context = isOBject(_context) ? _context : {}; | |
const fnName = checkRandomKey(getRandomKey(), context); | |
const args = Array.from(arguments).slice(1); | |
context[fnName] = this; | |
const result = context[fnName](...args); | |
delete context[fnName]; | |
return result; | |
}; | |
} | |
if(!Function.prototype.fauxApply){ | |
Function.prototype.fauxApply = function(_context, _args) { | |
const context = isOBject(_context) ? _context : {}; | |
const fnName = checkRandomKey(getRandomKey(), context); | |
const args = _args.length ? _args : [] | |
context[fnName] = this; | |
const result = context[fnName](...args); | |
delete context[fnName]; | |
return result; | |
}; | |
} | |
if(!Function.prototype.fauxBind){ | |
Function.prototype.fauxBind = function(_contetxt){ | |
const args = Array.from(arguments).slice(1); | |
const self = this; | |
return function(){ | |
return self.fauxApply(_contetxt,args) | |
} | |
} | |
} | |
function Product(name, price) { | |
this.name = name; | |
this.price = price; | |
} | |
function Food(name, price) { | |
Product.fauxCall(this, name, price); | |
this.category = "food"; | |
} | |
const feed = [2,4] | |
const y= 4; | |
const add = (a, b) => a + b; | |
console.log(new Food("cheese", 5)); // {name: 'chees', price: 5, category: 'food'} | |
console.log(add.fauxCall(null, 5, 6, 7)); // 11 :: 7 will ignore by add method | |
console.log(add.fauxApply(null, feed)); // 6 | |
console.log(add.fauxBind(null, 4,7)()); // 11 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment