【问题标题】:Javascript Object Passing Parameters [duplicate]Javascript对象传递参数[重复]
【发布时间】:2013-08-09 23:15:37
【问题描述】:

我写了一个对象,它工作正常,除了我不满意,因为我认为它可以改进:

这里是:

var Modal = {

    init: function (contact1,contact2,aboutus1,aboutus2,terms1,terms2,privacy1,privacy2) {

        $(".modaltrigger").removeAttr("target");

        $(".modaltrigger").click(function () {

            if ($(this).is("#contact")) {

                $('#primary_url').attr('href', contact1);
                $('#secondary_url').attr('href', contact2);

            } else if ($(this).is("#aboutus")) {

                $('#primary_url').attr('href', aboutus1);
                $('#secondary_url').attr('href', aboutus2);

            } else if ($(this).is("#termsconditions")) {

                $('#primary_url').attr('href', terms1);
                $('#secondary_url').attr('href', terms2);

            } else if ($(this).is("#privacy")) {

                $('#primary_url').attr('href', privacy1);
                $('#secondary_url').attr('href', privacy2);
            }
        });
    }
};

我将上面的方法调用为:

Modal.init("test.com","test2.com","test3.com", "test4.com","test5.com", "test6.com","test7.com","test8.com");

以上工作正常,但如果我有长字符串值要传递怎么办?有什么办法,我可以将变量声明为参数,并在调用方法时动态分配变量?我如何编写该代码..

我正在考虑将方法称为:

Modal.init({
 var 1: "firstsrting",
 var 2: "secondsting",
 ...
 ...
});

请帮忙..

【问题讨论】:

  • 您最后一个问题的公认答案难道没有准确回答这个问题吗?
  • 它没有按预期工作,因为在方法中声明的所有内容都覆盖了我在调用方法时设置的值:(
  • @Wasiim:再看看that answer。它确切地向您展示了如何做到这一点。
  • Modal.init({ contact1:"contact str", contact2:"contact str", ..... ..... lastitem : "last str" });
  • 方法调用时没有设置上述变量

标签: javascript jquery oop


【解决方案1】:

只使用对象作为一个参数。

var Modal = {

    init: function (params) {

        $(".modaltrigger").removeAttr("target");

        $(".modaltrigger").click(function () {

            if ($(this).is("#contact")) {

                $('#primary_url').attr('href', params.contact1);
                $('#secondary_url').attr('href', params.contact2);

            } else if ($(this).is("#aboutus")) {

                $('#primary_url').attr('href', params.aboutus1);
                $('#secondary_url').attr('href', params.aboutus2);

            } else if ($(this).is("#termsconditions")) {

                $('#primary_url').attr('href', params.terms1);
                $('#secondary_url').attr('href', params.terms2);

            } else if ($(this).is("#privacy")) {

                $('#primary_url').attr('href', params.privacy1);
                $('#secondary_url').attr('href', params.privacy2);
            }
        });
    }
};

并使用以下方法调用该函数(方法):

Modal.init({
    contact1: "http://test1.com",
    contact2: "http://test2.com",
    ...
    ...
});

【讨论】:

  • 太棒了..这很有效..但我可以看到自己重复一些代码,例如:在 else if - 我几乎在做同样的事情(同样的功能,只有参数在改变)。这可以进一步改进吗?
  • @Wasiim 如果您将 id 设置为与 param.<name><number> 中的名称相同,则可以缩小代码,因此当 id 为隐私时,参数将为隐私 1 和隐私 2。所以你可以使用var name = $(this).attr('id'); $('#primary_url').attr('href' params[name+'1']); $('#secondary_url').attr('href', params[name+'2']);而不用if。
猜你喜欢
  • 2014-06-07
  • 1970-01-01
  • 1970-01-01
  • 2017-03-29
  • 2012-05-27
  • 1970-01-01
  • 2011-09-15
  • 1970-01-01
  • 2018-08-02
相关资源
最近更新 更多