0

In my application, I receive a collection of Comments with JSON.

Then, I want to add functions to every item in this collection, so for example if a Comment has comment.first_name and comment.last_name, I want to build comment.full_name().

So far, I'm doing:

comment.full_name = function() { return this.first_name+" "+this.last_name }

My question is, since there are many comments (and other methods more complex than this one), how could I use prototypes or constructors to add the same function to every item in the collection, without inserting it into each element?

EDIT:

Here is an example of code:

comments = ({"1": {"first_name":"John", "last_name":"Silver"}, "2": {"first_name":"Jack", "last_name":"Sparrow"}})

Now, I'm looking for something that will return

comments[1].name() //=> John Silver
comments[2].name() //=> Jack Sparrow

2 Answers 2

1

To answer your direct question, you attach to an object's prototype by literally attaching it to is prototype chain with the prototype keyword. To attach a function, full_name() to the comment object's prototype, you would do something like this:

comment.prototype.full_name = function () {
  if (typeof(this.first_name) !== "undefined" && typeof(this.last_name) !== "undefined") {
    return this.first_name + " " + this.last_name;
  } else {
    return ""
  }
};

EDIT:

The above code will work for your situation as well. It will attach a property, full_name to each value in your JSON object. full_name is a function. It will return a concatenated string containing the current object's first_name property, a space, and the current object's last_name property, as long as both of the properties are not undefined. If one or both of them are, it will return an empty string.

Sign up to request clarification or add additional context in comments.

6 Comments

Great, that solves apart of my question! But wouldn't this add that method to the every object, besides comments? I would like to add that method only to comments, if I could specify (editing each comment) that only those objects should have the prototype, that would be perfect.
@micho I apologize, but I don't quite understand what you mean by every object. The above code will attach a function full_name to every comment object, and only to every comment object. Maybe you could revise your question to clarify exactly what you want?
@Alex. I'm not sure there is a comment type here. They just popped out of a JSON parser.
I edited my question. @spender is right, the prototype is undefined for comment in this case. The problem is that Comments are not being built from a constructor function, so we'd need to find a way of grouping them somehow.
@micho see my updated answer. Let me know if I'm still off-base.
|
1

Given that JSON does not encapsulate any info about type, I'm not sure you could do this generically without adding the method to the object prototype. Whether or not this is desirable is a subject for debate. Once you've got the data from the JSON parser, it's probably better to duck punch the objects one at a time (as it appears you are doing now).

2 Comments

But wouldn't that result in more memory comparison? I was thinking of replacing the constructor element for each comment and using a common prototype for all of them, but i have no idea of how to do that!
This tickled my interest, so I just asked another question about this: stackoverflow.com/questions/4299926/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.