Js函数将多个参数作为单个对象传递

时间:2015-06-03 16:07:03

标签: javascript

我有以下构造函数:

[<AbstractClass>]
type XBase () = 
    member val Name="" with get, set 

type X () = 
    inherit XBase ()  

type Y () = 
    inherit XBase ()  

//fine without tuples
let f(values:seq<XBase>) =       
    printf "ok."

f [new X(); new Y()]

//this function won't work 
let f1<'V when 'V :> XBase>(keysAndValues:seq<'V * string>) =       
    printf "ok."

//compiler error: This expression was expected to have type X but here it has type Y
//f1 [(new X(), ""); (new Y(), "")]

System.Console.ReadLine()

有没有办法将所有这些选择器作为单个对象传递?

Sub Update1()

    Dim Sh As Worksheet
    Dim Rng As Range
    Dim hL As Range

    Set Sh = Worksheets("macros")
    With Sh
        Set Rng = .Range("C2:C" & .Cells(.Rows.Count, "C").End(xlUp).Row)
    End With
    For Each hL In Rng
        ThisWorkbook.FollowHyperlink hL.Value
    Next hL

End Sub

2 个答案:

答案 0 :(得分:2)

根据ContentProvider的实施方式,您可以像这样使用Function.prototype.apply

HB.hideShowFacilites.Selectors

通过编辑它的定义,这种方法应该有效。

答案 1 :(得分:1)

在纯JS中,传递函数与参数列表是不可能的,一个对象的成员被视为参数,而不修改这样的函数:

HB.hideShowFacilites.Selectors = function(selectors){
    this.sel1 = selectors.sel1;
    this.sel2 = selectors.sel2;
    this.sel3 = selectors.sel3;
    this.sel4 = selectors.sel4;
    this.sel5 = selectors.sel5;
};

这样的函数需要一个参数,并将其视为sel1,sel2等字段的对象。

但反过来可以在这样的函数中使用传递的参数列表作为数组:

HB.hideShowFacilites.Selectors = function(sel1, sel2, sel3, sel4, sel5){
    this.sel1 = arguments[0];
    this.sel2 = arguments[1];
    this.sel3 = arguments[2];
    this.sel4 = arguments[3];
    this.sel5 = arguments[4];
};

此外,如果你不喜欢修改这个功能,可以用这样的东西重新定义它

HB.myHideShowFacilites = function(){};

HB.myHideShowFacilites.prototype = HB.hideShowFacilites;

HB.hideShowFacilites.Selectors = function(selectors){
    this.sel1 = selectors.sel1;
    this.sel2 = selectors.sel2;
    this.sel3 = selectors.sel3;
    this.sel4 = selectors.sel4;
    this.sel5 = selectors.sel5;
};

然后使用HB.myHideShowFacilites代替HB.hideShowFacilites