开发者

Executing Javascript functions by reference

I'm wondering if any of yall have any insight as to how one could execute a function by reference in javascript.

http://mootools.net/shell/yL93N/1/

Any discussion 开发者_如何学Pythonwould be cool.

-Chase


looking at your mooshell, the way i'd handle it in mootools is this:

http://mootools.net/shell/yL93N/10/

var proxyFunction = new Class({
    message: "hello",
    Binds: ['passByReference','sayit'],
    passByReference: function(func) {
        // console.log(this, this[func]);
        if (this[func] && $type(this[func]) === "function")
            this[func]();
    },
    sayit: function() {
        alert(this.message);
    },
    killit: function() {
        document.write('we\'re dead');
    }
});

$('tryit').addEvent('change',function(e){
    new proxyFunction().passByReference(this.get('value'));
});

// or have a permanent proxy instance if you call methods of the class often and need it to change things.

var proxy = new proxyFunction();
$('tryit').addEvent('change',function(e){
    proxy.passByReference(this.get('value'));
});

the advantage of doing so is that all your proxied functions are behind a common object, don't pollute your window namespace as global variables and can share data that relates to the event.


Not exactly sure what you mean, but you can do this:

var func = window.alert;
var args = ["hello world"]
func.apply(window, args)


Globally-defined functions (and variables) are visible as members of the global window object.

Members of an object can be fetched by name using the square bracket notation: o['k'] is the same as o.k. So, for your example:

var function_name= $(this).val();
window[function_name]();


Like this?

function blah() {
...do stuff
}

myref = blah

myref() 


The best way is to do:

func.call();


Function variables in JavaScript already are references. If you have a function:

var explode = function() { alert('boom!'); };

You can pass explode around as an argument, and it's only passing a handle to that function, not the entire function body.

For proof of this, try:

explode.id = 5;
var detonate = explode;
alert(detonate.id); // => 5
explode.id = 6;
alert(detonate.id); // => 6


functions are first class objects in Java Script. Effectively this means that you can treat it very much as if it were a variable, and pass it anywhere that you would expect a variable.

e.g.

var myFn = function() { alert('inside anonymous fn'); }

function callMyFn(paramFn) 
{
  paramFn();
}

callMyFn(myFn); //inside anonymous fn

function MyFnHolders(argFn)
{
  this.argFn = argFn;
  this.fieldFn = function() {
    alert('inside fn field');
  }
}

var myFnHolders = new MyFnHolders(myFn);
myFnHolders.argFn();   //'inside anonymous fn'
myFnHolders.fieldFn(); //'inside fn field'

//etc

so passing a function by ref can be done simply by assigning it to a variable and passing it around.


Here's one with a closure for your arguments...

function Runner(func, args) {
  return function() { return func.apply(window, args); };
}

var ref = new Runner(window.alert, ["hello world"]);
ref();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜