Pass object to javascript function

前端 未结 4 418
独厮守ぢ
独厮守ぢ 2020-12-08 02:22

I have recently been messing around with jQuery on my website, and I have a fairly limited knowledge of Javascript. I am beginning to like the jQuery ability to pass variabl

4条回答
  •  温柔的废话
    2020-12-08 02:29

    The "braces" are making an object literal, i.e. they create an object. It is one argument.

    Example:

    function someFunc(arg) {
        alert(arg.foo);
        alert(arg.bar);
    }
    
    someFunc({foo: "This", bar: "works!"});
    

    the object can be created beforehand as well:

    var someObject = {
        foo: "This", 
        bar: "works!"
    };
    
    someFunc(someObject);
    

    I recommend to read the MDN JavaScript Guide - Working with Objects.

提交回复
热议问题