主题:  function.prototype

s22

职务:版主
等级:4
金币:10.0
发贴:1634
#12003/3/11 12:50:02
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: hello world

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

trace(addTag("b","hello world"))
这样好像容易懂。
========
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; if.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");

编辑历史:[这消息被s22编辑过(编辑时间2003-03-17 09:38:14)]


s22

职务:版主
等级:4
金币:10.0
发贴:1634
#22003/3/11 12:51:06
Soft Shadow MX
I've just created a little prototype for a project that I want to share with you: it's a blurred rect drawing method which might come in useful if you want to draw soft shadows or glows. I think the peformance should be pretty ok as long as you do not use a lot of 50 pixel radians. I've created a little demo so you can see how it looks. Download the source here.http://www.quasimondo.com/archives/qm_shadow.fla




UndeadCraft

职务:版主
等级:4
金币:10.0
发贴:1993
#32003/3/12 8:37:54
function addTag(tag) {
var stag='<'+tag+'>';
var etag=''; //这句要改成var etag="<"+tag+"/>";
return function(x) {
return stag+x+etag;
}
}
trace(addTag("b")("hello world")) //第一次看到这种用法,汗.......


Function.prototype.loop=function(x) {
var f=this; //这句麻烦解释下先,不明白,TRACE的结果是type functon????有什么用?
return function() {
for (var i=0; if.apply(f,arguments);
}
}
}

function test(t){
trace(t)
}

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