Note that there are some explanatory texts on larger screens.

plurals
  1. POJavascript: How to turn a JSON array of object back in to the Object type sharing prototypes?
    primarykey
    data
    text
    <p>If you have an array of product objects created from JSON, how would you add a prototype method to the product objects so that they all point to the same method? How would you train JavaScript to recognize all product objects in an array are instances of the same class without recreating them?</p> <p>If I pull down a JSON array of Products for example, and want each product in the array to have a prototype method, how would I add the single prototype method to each copy of Product?</p> <p>I first thought to have a Product constructor that takes product JSON data as a parameter and returns a new Product with prototypes, etc. which would replace the data send from the server. I would think this would be impractical because you are recreating the objects. We just want to add functions common to all objects.</p> <p>Is it possible to <code>$.extend</code> an object's prototype properties to the JSON object so that each JSON object would refer to exactly the same functions (not a copy of)?</p> <p>For example:</p> <pre><code>var Products = []; Products[0] = {}; Products[0].ID = 7; Products[0].prototype.GetID = function() { return this.ID; }; Products[1].ID = 8; Products[1].prototype = Products[0].prototype; // ?? </code></pre> <p>I know that looks bad, but what if you JQuery <code>$.extend</code> the methods to each Product object prototype: create an object loaded with prototypes then <code>$.extend</code> that object over the existing Product objects? How would you code that? What are the better possibilities?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload