How to convert a literal object to a custom object in javascript?

Having written a ton of web applications using JSON / AJAX, I find that I am returning exact literal javascript objects (JSON). For example, I can request all cats from GetCats.asp. He will return:

[
  {'id': 0, 'name': 'Persian'},
  {'id': 1, 'name': 'Calico'},
  {'id': 2, 'name': 'Tabby'}
]

Now these are all Cat objects with behavior. However, if I define a Cat object, the Cat () {} function, I donโ€™t know an EFFECTIVE way to persuade these literals in the behavior of the user object.

I can do this by brute-force iterating through them and assigning functions, but that won't be pretty. Is there a good, one line (or several) way to somehow "distinguish" this behavior from these literals?

+3
source share
5 answers

Do not get around the fact that you have to go through all your simple objects and change them to another type of object. You cannot escape the cycle. With this, you can create a constructor that takes a simple object like this and copies these values โ€‹โ€‹to a new instance.

Like this:

function Cat(c) {
  this.id = c.id;
  this.name = c.name;
}
Cat.prototype.meow = function() {alert('meow');}
Cat.prototype.displayName= function() {alert(this.name);}

var cats = [
  { 'id': 0, 'name': 'Persian' },
  { 'id': 1, 'name': 'Calico' },
  { 'id': 2, 'name': 'Tabby' }
];

for (i=0,len=cats.length; i<len; i++) {
  cats[i] = new Cat(cats[i]);
}

cats[0].meow();  // displays "meow"
cats[0].displayName();  // display "Persian"
+2
source

If you are using json2 parser (or another with a compatible interface), you can simply provide a callback to replace raw objects with custom objects:

var catSource = '[ { "id": 0, "name": "Persian" }, { "id": 1, "name": "Calico" }, { "id": 2, "name": "Tabby" } ]';

function Cat(id, name)
{
   this.id = id;
   this.name = name;
}
Cat.prototype = 
{
   toString: function()
   {
      return this.name;
   }
};

function doStuff()
{
   var cats = JSON.parse(catSource, function(key, val)
   {
      // some expression to detect the type of val 
      if ( val.id !== undefined && val.name !== undefined )
         return new Cat(val.id, val.name);
      return val;
   });
   alert(cats);
}
0

? -


var cats = [
    {id: 15, name: 'Tables', count:45 },
    {id: 23, name: 'Chairs', count:34 }
];
var catsObjects = [];
cats.each(function(item){
    var newObject = new Cat();
    Object.extend(newObject, item);
    catsObjects.push(newObject);
});

Array.each , " < Array.length"
Object.extend - , newObject

0

@Benry.

, . - Object; Object.extend() (NB: , Object.prototype).

Object.extend = function ( take, give ) {
  for (var k in give) {
    if (give.hasOwnProperty(k)) {
      take[k] = give[k];
    }
  }
  return take;
}

, :

function Cat (c) {
  Object.extend( this, ( c || this.defaults )  );
}

Object.extend(Cat.prototype, {

  meow : function() {
    alert( 'Meow, my name is ' + this.name );
  },

  defaults : {
    name : 'I have no name', 
    id   : null
  }

});

:

var cats = [
  { 'id': 0, 'name': 'Persian' },
  { 'id': 1, 'name': 'Calico' },
  { 'id': 2, 'name': 'Tabby' }
];

for (i=0,len=cats.length; i<len; i++) {
  cats[i] = new Cat( cats[i] );
}

cats[0].meow();  // displays "meow, my name is Persian"
0

:

var list = [
  { 'id': 0, 'name': 'Persian' },
  { 'id': 1, 'name': 'Calico' },
  { 'id': 2, 'name': 'Tabby' }
];


for (obj in list)
{
  obj.prototype = new Cat();
}
-2

All Articles