Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best Way to Add Methods to Existing Javascript Object

I am receiving an ajax feed of documents that looks something like this (much simplified):

aDocs = [{title:'new doc', ext:'pdf'}, {title:'another', ext:'xlsx'}];

I am going to iterate through the aDocs array and display information about each doc, while adding some methods to each doc that will allow for modifying the HTML for display and making API calls to update the database.

I read here that in order to add methods to existing objects, you can use the __proto__ attribute. Something along the lines of:

function Doc(){}
Doc.prototype.getExt = function(){return this.ext}
Doc.prototype.getTitle = function(){return this.title}
for (var i=0; i<aDocs.length; i++){
  aDocs[i].__proto__ = Doc.prototype
}

According to that article above,this isn't official javascript, isn't supported by IE (never will be), and will likely be deprecated in webkit browsers.

Here's an alternative stab at it:

function getExt(){ return this.ext }
function getTitle(){return this.title}
for (var i=0; i<aDocs.length; i++){
  aDocs[i].getExt = getExt;
  aDocs[i].getTitle = getTitle;
}

Is this second alternative viable and efficient? Or am I re-creating those functions and thereby creating redundant overhead?

Again the above examples are simplified (I know aDocs[i].ext will solve the problem above, but my methods for display and API calls are more complicated).

like image 861
Eric H. Avatar asked Mar 12 '13 16:03

Eric H.


People also ask

Can methods be add to an object JavaScript?

JavaScript methods are actions that can be performed on objects. A JavaScript method is a property containing a function definition. Methods are functions stored as object properties.

How can we make methods available on all objects in JavaScript?

How can we make methods available on all objects? Explanation: It is possible to add methods to Object. prototype, making them available on all objects.

How do you add properties to an existing object?

One way is to add a property using the dot notation: obj. foo = 1; We added the foo property to the obj object above with value 1.

How can we add the custom property or method to all instances of an object?

Using the prototype object to add custom methods to objects The prototype object can also help you quickly add a custom method to an object that is reflected on all instances of it. To do so, simply create the object method as usual, but when attaching it to the object (you guessed it), use "prototype" beforehand.


1 Answers

Is this second alternative viable and efficient?

Yes.

Or am I re-creating those functions and thereby creating redundant overhead?

No, the functions are reused, not re-created. All of the objects will share the single copy of the getExt and getTitle functions. During the call to the functions from (say) aDocs[1], within the call, this will refer to the object the function is attached to. (This only applies if you call it as part of an expression retrieving it from the object, e.g., var title = aDocs[1].getTitle();)

Alternately, if you liked, you could create new objects which shared a prototype, and copy the properties from the aDocs objects to the new objects, but you've asked about assigning new functions to existing objects, so...

like image 133
T.J. Crowder Avatar answered Sep 16 '22 22:09

T.J. Crowder