Forums > Javascript

arguments.callee.task


August 17, 2006 | 7:32 pm

Hi,

If you want to access the Task from within a function, is there a reason
why should you use the syntax arguments.callee.task, rather than using
the variable name you assigned to the Task object?

For example, is there a difference between the first and the second example?

// example 1
var tsk = new Task(mytask, this);
function mytask
{
arguments.callee.task.cancel();
}

// example 2:
var tsk2 = new Task(mytask2, this);
function mytask2
{
tsk2.cancel();
}


Marco

November 30, 2011 | 12:47 pm

Came across this while looking for something else. In case anyone cares:

Example 2 won’t work if you use an anonymous function.

var tsktsk = new Task(function() { post("this function is has no namen"); });

I believe this functionality is mentioned in the documentation without a concrete example.

May 26, 2015 | 1:58 pm

Question on this – does this mean you can’t schedule an anonymous function? If not – how would you feed a defined function different arguments?

example:

var offset = 10;
var noteOut = new Task(function(){
messnamed("synth", 144,(24+offset),127);
});
noteOut.schedule(1000);

May 26, 2015 | 2:47 pm

feed it as an object Drkoss

May 26, 2015 | 2:57 pm

@DO…WHILE – thanks for the reply!

can you post a short example to point me in the right direction? I’m sure I’m missing the obvious here – but how would i ‘feed it as an object’?

thanks!

May 26, 2015 | 3:09 pm

yes sure .

var state = {value : 0};

var tick = function(st){
      post(st.value);
      st.value++;
      arguments.callee.task.schedule(266); 
}

var clock = new Task(this,undefined,state);

U define object , and pass it as a third argument . this way u can affect properties of an object . It will work with all reference types , like an array too .

May 26, 2015 | 3:12 pm

aha – thanks!

one question – it that a typo – should it read:

var clock = new Task(tick,undefined,state);

instead of

var clock = new Task(this,undefined,state);

again – many thanks!

May 26, 2015 | 3:18 pm

yes . sorry . "tick" for sure

May 26, 2015 | 3:32 pm

For some reason – doing that way acts like repeat – it will keep counting up instead of executing once. For anyone interested – this works for a single execution:

var state = {value : 0};

var tick = function(st){
post(st.value);
st.value++;
}

var clock = new Task(tick,undefined,state);

clock.schedule(500);

cheers!

May 26, 2015 | 4:09 pm

One problem – this doesn’t seem to work if the the var state is dynamic. It seems to define the function when compiled and no longer updates when executed later on (when state has become a different value)

any ideas?

thanks!

May 27, 2015 | 2:16 am

Your code would help here as i did not encountered any problems on my own .

var state = {value : 0};

var tick = function(st){
	post(st.value+"\n");
	st.value++;
	arguments.callee.task.schedule(500);
}

var clock = new Task(tick,this,state);


function bang(){
	clock.execute();
}

function msg_int(v){
	state.value = v ;
}

.
While setting value for the state object ,it will reflect inside tick function . I cant see reason why it would not work, unless u are setting your object to undefined . Treat your object as an argument holder .
post scriptum: dont be shocked while its repeating . its for the example purposes .

Viewing 11 posts - 1 through 11 (of 11 total)

Forums > Javascript