简体   繁体   English

如何使用函数向javascript对象添加值?

[英]How do i add a value to javascript object with a function?

bender: function () {
       var context = {
                pageName: 'Chocolatte  candy'

            },



            partials = {
                header: this.globalPartials.header,
                tabbar: this.globalPartials.tabbar
            };

          addBackbuttonIphone(); 

        $(this.el).html(templates["monkey"].render(context, partials));
        return this;
    }

});

return monkeyView;

});

in another location i have a js file that has the following function 在另一个位置,我有一个具有以下功能的js文件

function addBackbuttonIphone () {

    context.backButton="#more";


}

If i just add context.backButton="more" in var context directly it works . 如果我只是直接在var context中添加context.backButton =“ more”,那么它就可以工作。 However if i use the addBackButtonIphone function to do the same it does not work . 但是,如果我使用addBackButtonIphone函数执行相同操作,则它将不起作用。 I am sure that this function is being called however, it is not giving the "#more" value to context.backButton. 我确定该函数正在被调用,但是它没有为context.backButton提供“ #more”值。

Please help 请帮忙

Thanks 谢谢

modify your function signature to accept an argument called context , like this: 修改函数签名以接受称为context的参数,如下所示:

function addBackbuttonIphone(context) { 
  context.backButton="#more"; 
}

then pass context into your function, like this: 然后将context传递到您的函数中,如下所示:

addBackbuttonIphone(context);

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

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