Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6012535
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T02:25:06+00:00 2026-05-23T02:25:06+00:00

If you have an array of product objects created from JSON, how would you

  • 0

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?

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?

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.

Is it possible to $.extend 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)?

For example:

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 that looks bad, but what if you JQuery $.extend the methods to each Product object prototype: create an object loaded with prototypes then $.extend that object over the existing Product objects? How would you code that? What are the better possibilities?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T02:25:07+00:00Added an answer on May 23, 2026 at 2:25 am

    So, if I’m getting this all correctly, this is a more complete example of KOGI’s idea:

    // Create a person class
    function Person( firstName, lastName ) {
        var aPerson = {
            firstName:  firstName,
            lastName:   lastName
        }
    
        // Adds methods to an object to make it of type "person"
        aPerson = addPersonMethods( aPerson );
        return aPerson;
    }
    function addPersonMethods( obj ) {
        obj.nameFirstLast = personNameFirstLast;
        obj.nameLastFirst = personNameLastFirst;
        return obj;
    }
    function personNameFirstLast() {
        return this.firstName + ' ' + this.lastName;
    }
    function personNameLastFirst() {
        return this.lastName + ', ' + this.firstName;
    }
    

    So, with this structure, you are defining the methods to be added in the addPersonMethods function. This way, the methods of an object are defined in a single place and you can then do something like this:

    // Given a variable "json" with the person json data
    var personWithNoMethods = JSON.parse( json );    // Use whatever parser you want
    var person = addPersonMethods( personWithNoMethods );
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an array of objects that I'd like to filter using a predicate,
I have a data model which contains an array of Product objects. Before reporting
I have a object (product), with a property of type 'array' e.g. product.tags =
I have some Javascript code that creates 2 arrays: One for Product Category and
In kiama a generic dup method is defined which copies Product objects and applies
I have an array of objects and I want to convert it to an
I have the following code $result = $handle->select()->from('store_products_id', array('count'=>'COUNT(store_products_id.product_id)')) ->where('store_products_id.store_id=?', $this->store_id) ->columns($selectColumns) ->join('product_quickinfo', 'store_products_id.product_id
I have a UITableView which is populated with an array of objects of type
I would like to paginate my objects with the Kaminari pagination gem. I have
My company has a product that will I feel can benefit from a web

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.