1. // call
    2. Function.prototype.call = function(ctx, ...args) {
    3. ctx = ctx || window;
    4. const fnSymbol = Symbol('fn');
    5. ctx[fnSymbol] = this;
    6. ctx[fnSymbol](...args);
    7. delete ctx[fnSymbol];
    8. }
    9. // test
    10. function fn(c, d) {
    11. return this.a + c + d;
    12. }
    13. const obj = {
    14. a: 1
    15. }
    16. fn.call(obj, 2, 3); // 6
    1. // apply
    2. Function.prototype.apply = function(ctx, args) {
    3. ctx = ctx || window;
    4. const fnSymbol = Symbol('fn');
    5. ctx[fnSymbol] = this;
    6. ctx[fnSymbol](...args);
    7. delete ctx[fnSymbol];
    8. }
    9. // test
    10. function fn(c, d) {
    11. return this.a + c + d;
    12. }
    13. const obj = {
    14. a: 1
    15. }
    16. fn.apply(obj, [2, 3]); // 6
    1. // bind
    2. Function.prototype.bind = function(ctx, ...args) {
    3. ctx = ctx || window;
    4. const fnSymbol = Symbol('fn');
    5. ctx[fnSymbol] = this;
    6. return function (){
    7. ctx[fnSymbol](...args);
    8. delete ctx[fnSymbol];
    9. }
    10. }
    11. // test
    12. function fn(c, d) {
    13. return this.a + c + d;
    14. }
    15. const obj = {
    16. a: 1
    17. }
    18. fn.bind(obj, 2, 3); // 6