将普通对象转换为 javascript 中的类实例 [英] Casting plain objects to class instances in javascript

查看:26
本文介绍了将普通对象转换为 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 前 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 实例的创建工作类似.

The creation of the Animal instances works analogous.

由于 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天全站免登陆