最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - What does func.apply(this, arguments) do in this code for _.once()? - Stack Overflow

programmeradmin3浏览0评论

The following function underbar function was rewritten by peer of mine like so:

    var once = function(func) {
        var alreadyCalled = false;
        var result;

        return function() {
          if (!alreadyCalled) {
            result = func.apply(this, arguments);
            alreadyCalled = true;
          }
        return result
        };
      };

Here's how I interpret it. It's a function that takes another function and returns yet another function. If alreadyCalled is false then set result = func.apply(this,arguments)

Can someone please help me understand in a simple way what func.apply(this,arguments) is doing in the context of this function. I can't seem to figure it out!

The following function underbar function was rewritten by peer of mine like so:

    var once = function(func) {
        var alreadyCalled = false;
        var result;

        return function() {
          if (!alreadyCalled) {
            result = func.apply(this, arguments);
            alreadyCalled = true;
          }
        return result
        };
      };

Here's how I interpret it. It's a function that takes another function and returns yet another function. If alreadyCalled is false then set result = func.apply(this,arguments)

Can someone please help me understand in a simple way what func.apply(this,arguments) is doing in the context of this function. I can't seem to figure it out!

Share Improve this question edited Feb 12, 2015 at 5:11 Paul 27.4k13 gold badges89 silver badges126 bronze badges asked Feb 12, 2015 at 4:57 theamateurdataanalysttheamateurdataanalyst 2,8346 gold badges41 silver badges77 bronze badges 2
  • store this in variable previous return function... – Bhojendra Rauniyar Commented Feb 12, 2015 at 4:59
  • 2 MDN Apply – epascarello Commented Feb 12, 2015 at 4:59
Add a ment  | 

2 Answers 2

Reset to default 8

remove async await from

React.useEffect(async () => { await something},[]);

then it works just remove async await

React.useEffect(() => { something},[]);

I know your code is different but i got the same issue i remove async from all of the project which was used within useEffect and i dint saw the error again anymore

There are two implicit parameters on every function: this and arguments.

The apply method on the Function object lets you invoke it with those parameters explicitly set.

So what will happen here is that you a get a function wrapping another, and when you call it it will pass down the arguments to the original. It will also keep track if it was called and its result.

发布评论

评论列表(0)

  1. 暂无评论