如何初始化对象并分配不同的变量javascript [英] How to initialiaze an object and assign different variables javascript

查看:70
本文介绍了如何初始化对象并分配不同的变量javascript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个对象,我需要分配一些变量(参数),当调用对象时,变量(参数)会发生变化。这是我的代码:

I have created an object where i need to assign some variables(parameters) and when the object is called, the variables(parameters) change. Here is my code:

var Modal = {

    init: function () {

        contact1: "";
        contact2: "";
        aboutus1: "";
        aboutus2: "";
        privacy1: "";
        privacy2: "";
        terms1:   "";
        terms2:   "";

        $(".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);
            }
        });
    }
};

我正在尝试初始化上面的对象,但它不起作用:

I am trying to initialize the object above, and it does not work:

    Modal.init(

        contact1: "http:www.test1.com";
        contact2: "http:www.test2.com";
        aboutus1: "http:www.test3.com";
        aboutus2: "http:www.test4.com";
        privacy1: "http:www.test5.com";
        privacy2: "http:www.test6.com";
        terms1:   "http:www.test7.com";
        terms2:   "http:www.test8.com"

    );


推荐答案

它就像这样完成,

我猜这就是你想要做的。

i Guess this is what you want to do.

var Modal = {

    init: function (args) {

              //then access your values like this
            contact1=   args.contact1;
            contact2 = args.contact2;
            ..........
            .........
            .........
         }
}

要启动此方法,您必须写为

And to initiate this method you have write as

Modal.init({
contact1:"contact str",
contact2:"contact str",
.....
.....
lastitem : "last str"
});

这篇关于如何初始化对象并分配不同的变量javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆