在JavaScript中传递多个可选参数

在 Lua 中,我已经制作了自己的事件系统并希望将其转换并在 JavaScript 中使用,以下是 Lua 代码:

TriggerEvent = function(Name, ...)
    local EventID = nil;

    for Event2ID, Table in pairs(Event) do
        if(Table.Active and Table.Name == Name) then
            EventID = Event2ID;
            break;
        end
    end

    if(EventID == nil) then
        return false;
    end

    for ListenerID, Table in pairs(Event[EventID].Listener) do
        if(Table.Active) then
            Table.Function(...);
        end
    end
    return true;
end

它可以完美工作。 以下是 JavaScript 代码:

TriggerEvent = function(Name, ...) {
    var
        EventID = undefined
    ;

    for(Event2ID = 0, Length = Event.length; Event2ID < Length; Event2ID++) {
        if(Event[Event2ID].Active && Event[Event2ID].Name == Name) {
            EventID = Event2ID;
            break;
        }
    }

    if(EventID == undefined) {
        return false;
    }

    for(ListenerID = 0, Length = Event[EventID].Listener.length; ListenerID < Length; ListenerID++) {
        if(Event[EventID].Listener[ListenerID].Active) {
            Event[EventID].Listener[ListenerID].Function(...);
        }
    }
    return true;
}

它完全无法工作。

示例用法:

WebsiteConnected = function(Website, Test) {
    Website.SetTitle("Hello World");

    console.log(Website.GetTitle() + " connected! " + Test);
    return true;
}

CreateEvent("WebsiteConnected"); // 移至下面。
AddEventListener("WebsiteConnected", WebsiteConnected);
TriggerEvent("WebsiteConnected", (Website || {}), "Test"); // 移至下面。
点赞
用户238260
用户238260

以下是 Javascript 中“可选”参数的工作方式:

如果您编写以下函数:

function doSomething(arg1, arg2, arg3) {
    // 在此处插入优质代码 :-)
}

在 Javascript 中调用此函数(或任何其他函数)使用_任何_数量的参数都是有效的。

如果您传递少于三个指定的参数,则其余的参数将默认为空。

如果您传递超过三个参数,则额外的参数将被忽略。

如果您想“重载”函数,在 Javascript 中没有这样的东西。如果一个函数需要接受一个参数的两种不同类型,那么函数必须检查传递的参数类型并相应地行事。

另外,有 arguments 对象,它提供了一种类似数组的接口,用于传递给当前执行函数的参数。出于某种原因,在语言的开发中有一个小问题,即 arguments 对象的行为非常类似于数组,但它不是数组。 :-/

2014-08-19 14:18:12
用户2893193
用户2893193

我对之前所请求的方法进行了一些变通,这里是我的系统:

// ------------------------------------------------------------------------------------------------- Event "Class"
Event = function() {
    var
        Public = this,
        Private = {}
    ;

    Public.Create = function(Name) {
        if(Private.Created) {
            return false;
        }

        Private.Created = true;
        Private.Name = Name;
        Private.Function = [];
        return true;
    }

    Public.Delete = function() {
        if(! Private.Created) {
            return false;
        }

        Private = {};
        Private.Created = false;
        return true;
    }

    Public.IsCreated = function() {
        if(! Private.Created) {
            return false;
        }
        return true;
    }

    Public.SetName = function(Name) {
        if(! Private.Created) {
            return false;
        }

        Private.Name = Name;
        return true;
    }

    Public.GetName = function() {
        var
            Name = ""
        ;

        if(! Private.Created) {
            return Name;
        }

        Name = Private.Name;
        return Name;
    }

    Public.AddFunction = function(Function) {
        if(! Private.Created) {
            return false;
        }

        for(var FunctionID = 0, Length = Private.Function.length; FunctionID < Length; FunctionID++) {
            if(Private.Function[FunctionID] == Function) {
                return false;
            }
        }

        Private.Function[Private.Function.length] = Function;
        return true;
    }

    Public.HasFunction = function(Function) {
        if(! Private.Created) {
            return false;
        }

        for(var FunctionID = 0, Length = Private.Function.length; FunctionID < Length; FunctionID++) {
            if(Private.Function[FunctionID] == Function) {
                return true;
            }
        }
        return false;
    }

    Public.Call = function() {
        if(! Private.Created) {
            return false;
        }

        var
            Arguments = ""
        ;

        for(var argumentID = 0, Length = arguments.length; argumentID < Length; argumentID++) {
            Arguments += "arguments[" + argumentID + "]";

            if(argumentID != (arguments.length - 1)) {
                Arguments += ", ";
            }
        }

        for(var FunctionID = 0, Length = Private.Function.length; FunctionID < Length; FunctionID++) {
            eval("Private.Function[FunctionID](" + Arguments + ")");
        }
        return false;
    }

    Public.Create(arguments[0]);
    return Public;
}

var
    TestEvent = new Event("TestEvent")
;

TestEvent.AddFunction(
    function() {
        console.log("TestEvent called.");
    }
);

TestEvent.AddFunction(
    function(String, String1) {
        console.log(String1 + String);
    }
);

TestEvent.Call("world!", "Hello ");

这个系统表现得非常完美。我已经做了很多基准测试。

2014-12-31 21:17:56