serialization - A tricky javascript question -
I'll probably explain it through the best code I get something like this:
Var object 1 = function () {// do something} var object 2 = function () {// some more} var objects = {'o1': object1, 'o2': object 2}; Var verb = []; Function addAction (actionName) {var object = objects [actionName]; Actions.push (function () {new object ();}); }
So this code saves the runtime-defined actions sequence based on saved user input in an array.
addAction ("o1"); AddAction ("o2");
If I want to play that sequence again, then I just do it:
for (verb in i) {actions [i] () ; }
and it will create objects of two object 1 object and object 2.
Now, I need some sort of ordering [] array, but for me to maintain its scope, its inner work if I put the function in the string then I get
And if I remove this string, then the object is' Will be undefined. How would you do this ?
Why do not name the action with an object in the function, so that you can use the serialization For:
function action (name, func) {this.name = name; This.func = objects [name]; } AddAction function (actionName) {actions.push (new action (actionName)); }
The code will change slightly to run the functions:
for (verb i) verb [i] .func ();
To sort the array, you enter a name in only one string:
var s = ''; For (in verb) {s + = (s.length == 0? '': ',') + Verb [i] .name; }
Then to deserialise you rest the objects with only the names:
var name = s.split (','); For (i names) {addAction (new action (name [i])); }
Comments
Post a Comment