admin管理员组文章数量:1403200
I have created a JSON object, but within that object are a list of functions I want to have access to and run as a normal function. I'm trying to figure out how to acplish this, but I'm having difficulty. The following is what I'm doing:
Bootstrapper.dynamic = {
"interaction": function(data) {
s.linkTrackVars="events,prop2,eVar2,prop32,eVar32,prop33,eVar33";
s.linkTrackEvents="event2";
s.prop2="site:social:facebook";
s.eVar2="D=c2";
s.prop32=data.tp_type;
s.eVar32="D=c32";
s.prop33=data.ct_type;
s.eVar33="D=c33";
s.events="event2";
s.tl(this,'o','interaction');
}
};
Notice the "interaction" function. That's what I'm trying to fire, but am having difficulty. Thanks for any help.
I have created a JSON object, but within that object are a list of functions I want to have access to and run as a normal function. I'm trying to figure out how to acplish this, but I'm having difficulty. The following is what I'm doing:
Bootstrapper.dynamic = {
"interaction": function(data) {
s.linkTrackVars="events,prop2,eVar2,prop32,eVar32,prop33,eVar33";
s.linkTrackEvents="event2";
s.prop2="site:social:facebook";
s.eVar2="D=c2";
s.prop32=data.tp_type;
s.eVar32="D=c32";
s.prop33=data.ct_type;
s.eVar33="D=c33";
s.events="event2";
s.tl(this,'o','interaction');
}
};
Notice the "interaction" function. That's what I'm trying to fire, but am having difficulty. Thanks for any help.
Share Improve this question edited Mar 27, 2016 at 21:54 pingeyeg asked Mar 27, 2016 at 21:37 pingeyegpingeyeg 6902 gold badges9 silver badges26 bronze badges 5-
3
That's a JavaScript object, not a JSON object. To call the function, use
Bootstrapper.dynamic.interaction(yourData);
It's not clear whats
is supposed to be. – Pointy Commented Mar 27, 2016 at 21:38 -
1
For reference,
Bootstrapper.dynamic.interaction
is the same asBootstrapper.dynamic['interaction']
is the same asBootstrapper['dynamic']['interaction']
is the same asvar foo = 'dynamic', bar = 'interaction';
thenBootstrapper[foo][bar];
– Paul S. Commented Mar 27, 2016 at 21:45 - If I use Bootstrapper.dynamic['interaction'], how do I add arguments to it? – pingeyeg Commented Mar 27, 2016 at 21:54
-
@pingeyeg invoke as normal with parenthesis with ma-delimited args. However, if you are taking the reference to the function and holding that in a different identifier/parameter name which later is invoked, know the context has changed and hence the
this
object within the function may be different – Paul S. Commented Mar 27, 2016 at 22:21 - Thanks for the help on this one guys. Not sure why I got -2 points on this, since it was a legitimate question, but whatever. I'd like to award points, but I cannot yet. – pingeyeg Commented Mar 29, 2016 at 14:48
1 Answer
Reset to default 5Why exactly do you want to stringify the object? By design, JSON doesn't understand functions. However, Javascript objects do:
var x = {
name:"FirstName",
age:"21",
load:function(){ alert('hi') }
};
x.load(); //works
If you truly want to convert functions to JSON, take a look at JSONfn plugin: http://www.eslinstructor/jsonfn/
本文标签: javascriptExecute function within JSON objectStack Overflow
版权声明:本文标题:javascript - Execute function within JSON object - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744409152a2604862.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论