February 26, 2003
Fiddling With The Function Prototype

I came across this interesting thread on Flashforum.de (beware it's german). Florian Krüsch and Ralf Bokelberg are exploring the extension of the function prototype.

It starts with the basic idea that instead of an object or a variable you can as well return an anonymus function as the result of a function call:


function addTag(tag) {
var stag='<'+tag+'>';
var etag='';
return function(x) {
return stag+x+etag;
}
}

trace(addTag("b")("hello world"))
returns: <b>hello world</b>

But now you can also extend the Function object itself which will add "helper" functions to all other functions:


Function.prototype.loop=function(x) {
var f=this;
return function() {
for (var i=0; i<x; i++) {
f.apply(f,arguments);
}
}
}

function test(t){
trace(t)
}

test.loop(5)("hello world")

returns:
hello world
hello world
hello world
hello world
hello world

There are some nice things you can do with this method:


Function.prototype.executeLater = function() {
var func = this;
var timeToWait=arguments.shift();
var args=arguments;
var intervalId = setInterval(function () {
func.apply(f,args);
clearInterval(intervalId);
}, timeToWait);
};

Function.prototype.setDelay = function(timeToWait) {
var func = this;
return function() {
var args=arguments;
var intervalId = setInterval(function () {
func.apply(f,args);
clearInterval(intervalId);
}, timeToWait);
}
};

function test(){
trace("test " + arguments);
}

test.executeLater(100,"flory", "was", "here");

test.setDelay(1000)("flory","was","here","again");

testDelayed1000=test.setDelay(1000);

// now testDelayed has stored the delay and will always wait 1000ms
// before it calls test

testDelayed1000("flory","was","here","yet again");

Posted at February 26, 2003 11:55 AM | Further reading
Comments

Function.prototype.loop=function(x) {
var f=this;
return function() {
for (var i=0;i < x; i++) {
f.apply(f,arguments);
}
}
}

Posted by: mr. fixit on February 26, 2003 07:07 PM

Oh yes thanks - looks like HTML formatting intercepted it. I've fixed it.

Posted by: coma2mario on February 26, 2003 07:23 PM

These functions cause havoc with object methods. For example, if we have this code:

obj = new Object();
obj.prop = 5;
obj.traceProp = function() { trace(this.prop); };

obj.traceProp();
obj.traceProp.executeLater(50);

If it worked correctly we it should trace out
5
5

However, it traces out
5
undefined

which means when traceProp() is executed, it's not in the context of obj.

Posted by: Samuel Neff on February 26, 2003 11:28 PM

dddddd

Posted by: aaa on March 4, 2003 08:53 AM

very interesting :)
but you should fix a little mistake in the first function. Nothing serious.

var etag='';

Posted by: Simon on June 21, 2003 07:41 PM

the content of the var etag was deleted! I hope you know which mistake I meant!

Posted by: Simon on June 21, 2003 07:45 PM

-explain the purpose of this algorithm:

read m
for a = 1 to m
read x (a)
next a
read n
for b = 1 to n
read y (b)
next b
s = t = d = 1
while s y (t)
z (d) = y (t)
t = t + 1
d = d + 1
endif
else
z (d) = x (s)
s = s + 1
d = d + 1
endwhile
while m => s
z (d) = x (s)
d = d + 1
s = s + 1
endwhile
while n => t
z (d) = (t)
d = d + 1
t = t + 1
endwhile

-implement this algorithm and write down final contents of arrays x, y and z when the algorithm is executed with the following test data.

a) 3,1,4,9,3,2,3,7
b) 4,2,4,6,7,3,3,5,9

WALAS WAS HERE!

Posted by: Walas on January 13, 2004 12:00 PM
Post a comment
Name:


Email Address:


URL:


Comments:


Remember info?



Thank you!

Most Visited Entries
Sketches, Works & Source Code
Lectures
Contact
Backlog
In Love with
Powered by
Movable Type 2.661

© Copyright Mario Klingemann

Syndicate this site:
RSS 1.0 - RSS 2.0

Quasimondo @ flickr
Quasimondo @ LinkedIn
Quasimondo @ Twitter
Quasimondo @ Facebook
Quasimondo @ MySpace
Quasimondo is a Bright
Citizen of the TRansnational Republic
My other blog in german
Impressum


My family name is written Klingemann,
not Klingelmann, Klingeman, Klingaman, Kingemann,
Kindermann, Killingaman, Klingman, Klingmann, Klingonman
Klingemman, Cleangerman, Klingerman or Kleangerman

profile for Quasimondo at Stack Overflow, Q&A for professional and enthusiast programmers