Skip to main content
Stack Overflow
  1. About
  2. For Teams

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

Confuse about javascript function's call method

I read an article about how speed up javascript, I try copy it's code which could improve loop speed:

var chunk = function (array, process, context) {
 setTimeout(function(){ 
 var item = array.shift(); 
 console.log('item', item);//this could show correctly
 process.call(item, context); 
 if (array.length > 0){ 
 setTimeout(arguments.callee, 100); 
 } 
 }, 100); 
 }

Then I try to pass my parameter into it, but I don't know how to use the context parameter, what I have done is:

 var dosomething1 = function (item) {
 console.log('this is begin ' + item)
 }
 var dosomething2 = function (item) {
 console.log('this is end ' + item);
 }
 var process = function (item) {
 console.log(item); //this show undefined
 dosomething1(item);
 dosomething2(item);
 }
 var temp = ["a", "b", "c", "d"];
 chunk(temp, process);​

The problem is begin in the process function, the item log undefined, the item could only show correctly in chunk.

So how can I solve this problem?I think it related to the process.call method?Is it related to the context parameter?

You can see the demo here

Answer*

Draft saved
Draft discarded
Cancel

lang-js

AltStyle によって変換されたページ (->オリジナル) /