Destructuring a function from object ( Date Object )

后端 未结 2 1711
甜味超标
甜味超标 2021-01-27 00:45

If i want to destruct an Object i would do :

2条回答
  •  一整个雨季
    2021-01-27 01:17

    Because this it not a Date object. When you call getDate() without its proper context (ie. date.getDate()), then you're calling it in the context of the window (or null in strict mode). Neither window nor null are Date objects, therefore the function fails.

    Try const getDate = date.getDate.bind(date);

    Demo:

    const test = { fn : function() { return this.constructor; } };
    
    const normal = test.fn();
    console.log(normal); // object
    
    const {fn} = test;
    console.log( fn() ); // window
    
    const bound = test.fn.bind(test);
    console.log( bound() ); // object

提交回复
热议问题