将普通对象投射到javascript中的类实例 [英] Casting plain objects to class instances in javascript

查看:48
本文介绍了将普通对象投射到javascript中的类实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

function Person() {
      var self = this;

      self.personName="";
      self.animals=[];
}

function Animal(){
     var self=this;

     self.animalName="";
     self.run=function(meters){
         .....
     }
}

服务器响应:

 [{personName:John,animals:[{animalName:cheetah},{animalName:giraffe}]} , {personName:Smith,animals:[{animalName:cat},{animalName:dog}]} ]

我正在从服务器获取Person数组.我想将通用的Person数组转换为输入的Person数组.所以我可以使用

I'm getting Person array from server. I want to cast generic Person array to typed Person array. So I can use

 persons[0].Animals[2].Run();

我创建了Javascript

I founded Javascript's

 Object.create(Person,person1);

但是我想要具有浏览器支持的跨浏览器版本

But I want cross-browser version of it with array support

  ObjectArray.create(Person,persons);

 Object.create(Person[],persons);

推荐答案

在JavaScript中创建对象需要调用其构造函数.因此,首先,您将需要找到正确的参数,而这些参数可能并不总是仅仅是属性.之后,您可以将所有公共属性从JSON分析的对象重新分配给创建的实例.

Creating an object in JavaScript requires the invocation of its constructor. So, at first you will need to find the correct arguments, which may not always be just properties. After that, you can reassign all public properties from the JSON-parsed object to the created instances.

一个通用的解决方案是,每个构造函数都接受看起来像实例(包括真实实例)的任何对象并对其进行克隆.这样,创建正确实例所需的所有内部逻辑都将位于正确的位置.

A general solution would be that every constructor accepts any objects that look like instances (including real instances) and clones them. All the internal logic needed to create proper instances will be located in the right place then.

甚至比重载构造函数更好的方法是在类上创建一个静态方法,该方法接收对象并从中创建实例:

Or even better than overloading the constructor might be to create a static method on your class that takes objects and creates instances from them:

Person.fromJSON = function(obj) {
    // custom code, as appropriate for Person instances
    // might invoke `new Person`
    return …;
};


您的情况非常简单,因为您没有任何参数,只有公共属性.要将{personName:John,animals:[]}更改为对象实例,请使用以下命令:


Your case is very simple, as you don't have any arguments and only public properties. To change {personName:John,animals:[]} to an object instance, use this:

var personLiteral = ... // JSON.parse("...");
var personInstance = new Person();
for (var prop in personLiteral)
    personInstance[prop] = personLiteral[prop];

您还可以为此使用Object.assign功能(例如,jQuery.extend pre-ES6):

You can also use Object.assign functionality (or e.g. jQuery.extend pre-ES6) for this:

var personInstance = Object.assign(new Person(), personLiteral);

Animal实例的创建类似.

由于JSON不会传输有关类的任何信息,因此您必须事先了解其结构.在您的情况下,它将是:

As JSON does not transport any information about the classes, you must know the structure before. In your case it will be:

var persons = JSON.parse(serverResponse);
for (var i=0; i<persons.length; i++) {
    persons[i] = $.extend(new Person, persons[i]);
    for (var j=0; j<persons[i].animals; j++) {
        persons[i].animals[j] = $.extend(new Animal, persons[i].animals[j]);
    }
}

顺便说一句,您的run方法似乎很可能添加在Animal.prototype对象上,而不是每个实例上.

Btw, your run methods seems likely to be added on the Animal.prototype object instead of each instance.

这篇关于将普通对象投射到javascript中的类实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆