So far I haven't been able to grok any Delegate function tutorials out there for Javascript. Even less so the ones with pa开发者_如何学Crameters.
Hopefully my current problem will provide a good example. (XXXX() is a function I am passing to Fee())
The way I have it now, my "delegate", (XXXX()) executes as soon as Fee() is called, not when I want it to be called (somewhere inside of Fee())
line of code that calls function:
Fee(prod, item, "usage", i, y, XXXX(prod, y));
Function:
function Fee(prod, item, name, i, y, func) {
    var open = false;
    var prefix = "#" + prod + name;
    $(prefix + y).click(function () {
        var feeBoxName = prefix + "FeeBox" + y;
        if (open == false) {
            FeeOpen(prefix, y);
            func; //delegate is called here
            AddFeeButton(feeBoxName, "addFeeBtn2");
            open = true;
        } else {
            FeeClosed(prefix, y);
            open = false;
        }
    });
}
Delegate function:
function XXXX(prod, y) {
    var deducedFeeBoxName = "#" + prod + "usageFeeBox" + y;
    alert(deducedFeeBoxName); //at present, this is called immediately when Fee is executed 
    UsageFeeBoxHeader(deducedFeeBoxName);
    UsageFeeBoxData(deducedFeeBoxName, prod, y);
}
Comments?
You want:
Fee(prod, item, "usage", i, y, function() { XXXX(prod, y); });
It's usually not called a "delegate" in Javascript. I believe that's a term mostly used in the Microsoft part of the world. We just call it a lambda or anonymous function.
You also have a bug in the main (Fee) function. Change this line:
func(); //delegate is called here
If your function is called XXX, then if you use this syntax: XXX(), you are invoking your function. To pass it in as a delegate, you simply pass in XXX:
Fee(prod, item, "usage", i, y, XXX);
The Fee method will be responsible for invoking it and determining which parameters are passed into the method.
                                        
                                        
                                        
                                        
                                        
                                        
                                        
                                        
 加载中,请稍侯......
      
精彩评论