Created
June 26, 2018 18:11
-
-
Save ahy4/e2eefea4d62b6926759249609026204e to your computer and use it in GitHub Desktop.
This file contains hidden or 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
// ns.targetFnを上書きしたい | |
var ns = { | |
prop: 10 | |
}; | |
ns.__proto__.targetFn = function targetFn(v1, v2) { | |
return this.prop + v1 + v2 | |
}; | |
console.log(ns.targetFn(5, 3)); // => 18 | |
var tmp1 = ns.targetFn; | |
var tmp2 = function targetFn() { | |
console.log('my custom call'); // この辺で上書き前のfunctionのコピーを取ったり、on,emitをすればいいと思う | |
return tmp1.apply(ns, arguments); | |
}; | |
console.log(tmp2(5, 3)); // => my custom call 18 | |
Object.defineProperty(ns, 'targetFn', { | |
value: tmp2, | |
enumerable: ns.propertyIsEnumerable('targetFn'), | |
configurable: true, | |
writable: true | |
}); | |
console.log(ns.targetFn(5, 3)); // => my custom call 18 | |
console.log(ns.targetFn.name); // => targetFn | |
console.log(ns.propertyIsEnumerable('targetFn')); // => false |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment