Heim > Web-Frontend > js-Tutorial > Hauptteil

Wie wandelt man Objekte in Instanzobjekte um und aktiviert die Methodenverwendung in JavaScript-Arrays?

Patricia Arquette
Freigeben: 2024-10-18 12:22:30
Original
261 Leute haben es durchsucht

How to Cast Objects to Instance Objects and Enable Method Use in JavaScript Arrays?

Casting Objects to Class Instances in JavaScript

Question:

How can I cast a generic object array (e.g., [{personName: 'John', animals: []}]) received from a server to a typed array of objects (e.g., [Person(personName: 'John', animals: [])]), allowing for method use (e.g., persons[0].animals[2].run())?

Answer:

Creating class instances in JavaScript requires invoking the constructor with the correct arguments (not just properties). We provide two solutions:

Method 1: Static Method for Cloning

Define a static method on the class that takes objects and creates instances from them, for example:

<code class="javascript">Person.fromJSON = function(obj) {
  // Custom code for Person instances
  return new Person();
};</code>
Nach dem Login kopieren

Method 2: Object Cloning

Clone the properties from the JSON-parsed object to a new instance, for example:

<code class="javascript">var personLiteral = JSON.parse("...");
var personInstance = new Person();
for (var prop in personLiteral)
  personInstance[prop] = personLiteral[prop];</code>
Nach dem Login kopieren

Or, using Object.assign:

<code class="javascript">var personInstance = Object.assign(new Person(), personLiteral);</code>
Nach dem Login kopieren

Example for Your Case:

In your specific case, you have a simple object structure with no arguments and only public properties. To convert to an object instance:

<code class="javascript">var persons = JSON.parse(serverResponse);
for (var i=0; i&lt;persons.length; i++) {
  persons[i] = $.extend(new Person, persons[i]);
  for (var j=0; j&lt;persons[i].animals; j++) {
    persons[i].animals[j] = $.extend(new Animal, persons[i].animals[j]);
  }
}</code>
Nach dem Login kopieren

Note: The run method should likely be added to the Animal.prototype object instead of each instance.

Das obige ist der detaillierte Inhalt vonWie wandelt man Objekte in Instanzobjekte um und aktiviert die Methodenverwendung in JavaScript-Arrays?. Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!

Quelle:php
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Neueste Artikel des Autors
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage
Über uns Haftungsausschluss Sitemap
Chinesische PHP-Website:Online-PHP-Schulung für das Gemeinwohl,Helfen Sie PHP-Lernenden, sich schnell weiterzuentwickeln!