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 what s is supposed to be. – Pointy Commented Mar 27, 2016 at 21:38
  • 1 For reference, Bootstrapper.dynamic.interaction is the same as Bootstrapper.dynamic['interaction'] is the same as Bootstrapper['dynamic']['interaction'] is the same as var foo = 'dynamic', bar = 'interaction'; then Bootstrapper[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
Add a ment  | 

1 Answer 1

Reset to default 5

Why 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