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 teach JavaScript to recognize all the product objects in an array, are instances of the same class without re-creating them?
If I pulled out an array of JSON products, for example, and I want every product in the array to have a prototype method, how would I add one prototype method to each copy of the product?
At first I thought that I had a Product constructor that takes JSON product data as a parameter and returns a new product with prototypes, etc., which will replace the data transfer from the server. I would think that it would be impractical because you recreate objects. We just want to add features common to all objects.
Is it possible for the $.extend property of an object prototype to be a JSON object so that each JSON object refers to exactly the same functions (not a copy)?
For instance:
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;
I know this looks bad, but what if you have jQuery $.extend methods for each prototype of a product object: create an object loaded with prototypes and then $.extend this object on top of existing Product objects? How would you encode this? What are the best features?
source share