I have a bunch of JSON string returned from an ajax call in a specific format and when starting to convert them all into my own Javascript object, I start to wonder if there is any easier way since we're talking Javascript here.
I'll have var manyOfThem = [ { name: 'a' }, { name: 'b' }, { name: 'c' } ];
And I'd like to easily associate each of these objects with my functions so that I can do things like:
myClass.prototype.doSomething = function() {
// do something to this.name
};
$.each(manyOfThem, function(index, item) {
item.doSomething();
});
I guess my concern is, I would not want to (because its repetitive) do this:
var myClass = function(item) {
this.name = item.name;
// do the same for the rest of item's potentially 20 properties
};
var oneOfThem = new myClass(manyOfThem[0]); // I think this is redundant....
oneOfThem.doSomething();
Anyhow, if there is also (security?) reasons why I'd just have to suck it up and do them all manually please share as well, thanks!
doSomething. Can you share what your plan is? Like why couldn't you just make afunctioncalleddoSomethingand passitemto it?myClassaccept an array (manyOfThemin this case). Then, when you want to manipulate one of its items (calling somedoSomethingfunction), you could domyClass.doSomething(3)meaning the 3rd index. Then internally, it would just operate on the 3rd item in the array (without exposing the array).doSomethingwas a function outside anything it would be 1. in the global namespace, 2. counter-object-oriented design