Store reference to `call` function

前端 未结 2 941
[愿得一人]
[愿得一人] 2020-12-21 21:22

I noticed something curious earlier today. I can\'t seem to store a reference to the call property of a function, then execute it. Example:

var          


        
2条回答
  •  Happy的楠姐
    2020-12-21 22:10

    You need to keep the binding to console. Try this:

    var logCall = console.log.call.bind(console.log);
    // example: logCall(console, "foobar");
    

    or

    var log = console.log.bind(console);
    // example: log("foobar");
    

    For a bound reference to log.

    Edit: jsfiddle: http://jsfiddle.net/67mfQ/2/

提交回复
热议问题