简体   繁体   English

我可以将变量合并为javascript对象吗?

[英]Can I combine variables into a javascript object?

I have the following code: 我有以下代码:

var submitHandler = function ($link, $form, close) {

   var val = $form.valid();
   var action = $form.attr('data-action')
   var entity = $form.attr('data-entity')
   var href = $form.attr('data-href');
   var rownum = $link.attr('data-row');
   var $row = $('#row_' + rownum);
   var $submitBt = $('.block-footer button:contains("Submit")');
   .....
   .....
   jsonSuccessModal(action, rownum, close, $form, $submitBt, $row);

}

function jsonSuccessModal(action, rownum, close, $form, $submitBt, $row) {
    ...
}

Rather than have many parameters is there a way I could combine variables in the first code block and then just send an object that represents the combination to the jsonSuccessModal. 没有很多参数,我可以在第一个代码块中组合变量,然后将代表组合的对象发送到jsonSuccessModal。 Something like an anonymous object ? 像匿名对象一样?

var submitHandler = function (data) {
    data.link; // Do something...
    data.form; // Do something...
    ...
};

submitHandler({
    link: "foo",
    form: "form",
    close: "close"
});​

You could create your own object and send that over. 您可以创建自己的对象并将其发送。 it would also be easier when wanting to access the data. 当想要访问数据时,它也将更加容易。 http://www.howtocreate.co.uk/tutorials/javascript/objects shows how to make your own object http://www.howtocreate.co.uk/tutorials/javascript/objects显示了如何制作自己的对象

Here's a pattern i've come to use on functions that take a lot of parameters (uses jQuery, though): 这是我在需要大量参数的函数上使用的一种模式(不过使用jQuery):

var myFunc = function (parameters) {
    // any parameters not passed will be defaulted here
    parameters = $.extend({
        foo: 1,
        bar: 'hello',
        biz: false
    }, parameters || {}); // default parameters to an empty object if it is not passed at all

    // use the parameters like you normally would
    var answer = 6 + parameters.foo;
};

// call the function using an object
myFunc({ foo: 2, bar: 'goodbye'}); // notice biz will be defaulted to false

if you do not want to use jquery, you can use something like this instead of $.extend (untested) 如果您不想使用jquery,则可以使用类似这样的方法来代替$.extend (未经测试)

var extend = function (defaultObject, sourceObject) {
    for (var key in defaultObject) {
        if (!sourceObject.hasOwnProperty(key)) {
            sourceObject[key] = defaultObject[key];
        }
    }
    return sourceObject;
};

In the general case the answer to your question is NO -- javascript doesn't support keyword arguments unpacking like eg in Python ( **kwargs ). 在一般情况下,您问题的答案为否-javascript不支持像Python( **kwargs )这样的关键字参数解包。 You could try "extending" Javascript to support this kind of feature, but the code will be rather awkward (since this language has no reflection support either): 您可以尝试“扩展” JavaScript以支持这种功能,但是代码会很尴尬(因为该语言也没有反射支持):

function paramNames(func) {
    return func.toString().match(/\(([^)]+)\)/)[1].split(/[\s,]+/)
}

function callWithKwargs(func, args, thisptr) {
    return func.apply(thisptr || this,
        paramNames(func).map(function(e) { return args[e] }));
}

// example:

function foo(bar, baz, spam) {
    console.log('bar', bar, 'baz', baz, spam, 'spam'); 
}

callWithKwargs(foo, {
    spam: 'bacon',
    baz: 'hello',
    bar: 'barbar'
})

That said, if the function in question is yours, you can simply pass an object to it as already suggested. 就是说,如果所涉及的函数是您的函数,则可以按照建议将对象简单地传递给它。

Yes you could do it like below 是的,您可以像下面这样

var submitHandler = function ($link, $form, close) {
   var obj ={
 val = $form.valid();
 action = $form.attr('data-action')
 entity = $form.attr('data-entity')
 href = $form.attr('data-href');
 rownum = $link.attr('data-row');
 $row = $('#row_' + rownum);
 $submitBt = $('.block-footer button:contains("Submit")');};
   jsonSuccessModal(obj);

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM