簡體   English   中英

在JavaScript中使用可變數量的參數調用函數(類似於call())

[英]Call a function with a variable number of arguments in JavaScript (similar to call())

我熟悉call()的方式,您可以傳遞可變數量的參數,這些參數將在調用時加載到函數的參數中。 我試圖做相關的東西,我通過嵌套組對象遞歸在RaphaelJS用forEach (類似於jQuery的each ),確定子元素是否是另一套,並應用函數的如果沒有參數個數可變。 我想使其通用,以便我可以應用任何函數,但使傳遞的函數具有簡單的參數構造函數,而不必訪問該arguments屬性。

function recursiveFncApply(set, fnc, args) {
    set.forEach(function(item) {
        if (item.type == 'set') {
            recurseApplyFncToSets(item, fnc, args);
        } else {
            fnc(item, args);
        }
    });
}

function translateOperation(element, operation, x, y)
    // do stuff to element with operation, x, and y args without accessing
    // accessing arguments property
}

recursiveFncApply(passedSet, translateOperation, [arg1, [arg2, ...]]);

我想這樣做,以便可以使用多個功能,而不必重復使用確定參數並在使用前正確分配參數的代碼。 我不確定是否缺少某種功能或語言實用程序使我能夠做到這一點,或者不確定是否可以通過傳遞給recursiveFncApply的其余參數以編程方式“構造”函數調用。 這在JavaScript中可行嗎?

澄清:我想將可變數量的參數傳遞給遞歸函數,該參數將傳遞給要應用於遞歸函數正在處理的集合內容的任何函數。 因此,我希望能夠使recursiveFncApply與任何函數通用地工作,同時仍使用類似於通過call()執行的函數那樣的參數結構。

說我除了translateOperation還有另一個功能:

function anotherFunction(element, differentArg) {
    // do something with one argument
}

理想情況下,我可以按以下方式使用我的recursiveFncApply

recursiveFncApply(passedSet, translateOperation, operation, x, y);
recursiveFncApply(passedSet, anotherFunction, singleArg);

以及這種方式:

recursiveFncApply(passedSet, anotherFunction, singleArg);

我認為這與call()工作方式類似,我可以這樣做:

anotherFunction.call(this, element, differentArg);

..無需更改anotherFunction的結構即可整理arguments屬性或傳遞對象/數組。

事實證明,菲利克斯·金(Felix King)有正確的想法/是最接近的想法。 我一意識到我實際上想做的事,便立即找到問題的直接答案,這是將參數從一個函數傳遞到另一個函數(在此處找到答案)。 因此,我將其與以下代碼一起使用:

function recursiveSetFncApply(set, fnc/*, variable */) {
    var me = this;
    var parentArgs = arguments;
    set.forEach(function(element) {
        if (element.type == 'set') {
            parentArgs[0] = element;
            me._recursiveSetFncApply.apply(me, parentArgs);
        } else {
            // Generate args from optional arguments and pass forward; put element in args at front
            var args = Array.prototype.slice.call(parentArgs, 2);
            args.unshift(element);
            fnc.apply(element, args);
        }
    });
}

我使用parentArgs引用了arguments ,因為我需要先更新argumentsset屬性,然后再將其傳遞到下一個遞歸循環,否則會遇到無限循環,因為它根本沒有更新,因為它使用的是原始參數集。 我的印象是apply()實際上不會傳遞參數,而只是將數組彈出到必須通過索引訪問的新函數中,實際情況並非如此。 當我在translateElementOperation上使用apply()時,我將所有需要的參數放在它們的確切位置。 這是我通過遞歸應用程序運行的更新函數:

function translateElementOperation(element, operation, x, y) {
    var currentPath = element.attr('path');
    translatedPath  = Raphael.transformPath(currentPath, [operation, x, y]);
    element.attr('path', translatedPath);
}

謝謝大家的幫助!

使用.apply的,而不是.call

functionName.apply(element, [any, number, of, variables, ...]);

// instead of this
functionName.apply(element, set, of, variables, ...);

這樣更有用:

var fnVars = [];// fill this anyway you want.

functionName.apply(element, fnVars);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM