具有两个不同功能的参数的调用函数

时间:2016-02-25 11:42:20

标签: javascript function

我有两个不同的函数来获取参数:

push.on('registration', function(data) {
        var id = data.registrationId;
        });


push.on('notification', function (data) {
        var count = data.count;
        });

现在我想在另一个新函数中使用变量id和count:

function three(id, count){

var _id = id;
var _count = count;

}

这怎么可能?

4 个答案:

答案 0 :(得分:3)

Sub Sample()
Dim wsI As Worksheet, wsO As Worksheet
Dim lRow_I As Long, lRow_O As Long, i As Long, nRowsToPaste As Long
Dim rngToCopy As Range, rngToPaste As Range

'~~> Set your input and output sheets
Set wsI = ThisWorkbook.Sheets("SheetI")
Set wsO = ThisWorkbook.Sheets("SheetO") '<=== I made it different that wsI

'~~> Output row
lRow_O = wsO.Range("A" & wsO.Rows.Count).End(xlUp).row + 1
With wsI
    '~~> Get last row of input sheet
    lRow_I = .Range("A" & .Rows.Count).End(xlUp).row

    '~~> Loop through the rows
    For i = 2 To lRow_I
        nRowsToPaste = val(Trim(.Range("M" & i).Value)) '<== set number of rows to be pasted

        Set rngToCopy = .Range(.Cells(i, 1), .Cells(i, wsI.Columns.Count).End(xlToLeft)) '<== set range to be copied
        Set rngToPaste = wsO.Rows(lRow_O).Resize(1, rngToCopy.Columns.Count)             '<== set 1st row of the range to be pasted

        rngToCopy.Copy rngToPaste      '<== copy&paste the 1st row in wsO sheet                                                        '<== copy and paste the 1st row
        Call Prefix(rngToPaste) '<== differentiate each single cell of pasted range by means of adding a different prefix. this will subsequently have autofill method work on cells with originally the same value as well

        With rngToPaste
            .AutoFill .Resize(nRowsToPaste + 1) ' <== fill all rows exploiting AutoFill method, which will work on every column being their 1st row different from each other
            .Resize(nRowsToPaste + 1).Replace What:="%%*%%", Replacement:="", LookAt:=xlPart '<== remove prefix
        End With

        lRow_O = lRow_O + nRowsToPaste + 1 '<== GET the next output row

    Next i

End With
End Sub

Sub Prefix(rng As Range)
Dim j As Long
With rng
    For j = 1 To .Columns.Count
        .Cells(1, j).Value = "%%" & j & "%%" & .Cells(1, j).Value
    Next j
End With
End Sub

现在您可以调用if ( (b[2] <= c[2]) && (b[2] >= a[2]) ) //comparing years if ( (b[1] <= c[1]) && (b[1] >= a[1]) ) //comparing months if ( (b[0] <= c[0]) && (b[0] >= a[0]) ) //comparing days // ... b is between a and c, so do something .... 。问题是,在调用var id, count; push.on('registration', function(data) { id = data.registrationId; }); push.on('notification', function (data) { count = data.count; }); 之前,您必须等到两个值都存在。可能你正在寻找这些方面的东西:

three(id, count)

答案 1 :(得分:0)

push.on('registration', function(data) {
    var id = data.registrationId;
    push.on('notification', function (data) {
        var count = data.count;
        three(id,count)
    });

 });

基于评论的更新: 假设事件的顺序,

答案 2 :(得分:0)

如果您不确定事件发生的顺序,您可能需要在其功能范围之外获取idcount,然后,当一个推送功能触发时,检查是否其他人有,如果有,请拨打three(id, count)。一种方法是仅在函数之外声明变量,然后相应地更新它们的值。

var id = null;
var count = null;

push.on('registration', function(data) {
        id = data.registrationId;
        count !== null ? three(id, count) : null
        });


push.on('notification', function (data) {
        count = data.count;
        id !== null ? three(id, count) : null
        });

答案 3 :(得分:0)

你不能在函数three()中使用'id'和'count'变量的原因是因为它们是在各个函数的范围内声明的。要在函数three()中使用它们,只需将其移动到更高的范围。

以下是我如何解决您的问题:

int a = Integer.parseInt(holders.replace(",", ""));