Wednesday 15 August 2012

Combining prototype and "class" inheritance in JavaScript -


In my Javascript application, I have several types of objects, in which query , Document and Posting . A document contains a bunch of metadata about a document (title, abstract, etc.); A posting represents some of the documents received by some queries on some ranks. The same documents can be retrieved by more than one question, and thus more than one posting may be related to it.

The app has two relevant ideas: One that shows all the posting for a given question, and one which displays all the documents since the user can communicate in two ways in the same way, so I decided to use prototype inheritance by making prototypes of one or more posting examples as a document example. In this way, postings get all the metadata from the document, and simply adds your query reference and rank value. Works like a charm.

In addition to data (question, rank) posting, some behavior is also done. I wanted to keep this behavior in mind so that I do not have thousands of similar methods. In the case of the document, I just moved all my works to document.prototype . But in the case of posting, I can not do this because every posting has a different prototype (its related document). While I can put some methods used by posting in document.prototype and get it on them like this, some methods are polymorphism and need to be implemented separately for documentation and posting. is.

My question is this: If I am using prototype inheritance in posting to continue data , should I somehow behave Can work so that I can reuse it

  function document (id, title) {this.id = id ;, when I am creating a new posting I This.title = Title; } // Here prototype is used to streamline the behavior of Document.prototype.document = function () {returned; } Document.prototype.toString = function () {return 'document [' + this.id + ']: "' + this.title + '' '';} Posting function (query, rank) {this.query = query ; This.rank = rank; // I can move these two definitions out of the example example / I move to create several copies of these tasks because I create 1000s of posting? This.document = function () {Return Object.getPrototypeOf (this);} This.toString = function () {return.document (). ToString () + 'on rank' + this.rank;}} Create posting (query, diarrhea) Waze, rank) {// Here prototype is used to posting data. Prototype = document; var posting = new posting (query, rank); returning posting;}   

UPDATE

I made a mistake in the example code above: The correct method of inheritance (as mentioned in the comment below) is to set the first prototype: posting .prototype = document; I think my rest Sn are still valid: -)

If you do not save Why functions outside manufacturer posting.

  function document (id, title) {this.id = id; This.title = Title; } // Here prototype is used to streamline the behavior of Document.prototype.document = function () {returned; } Document.prototype.toString = function () {return 'document [' + this.id + ']: "' + this.title + '' ';;} Posting tootring (function) {Return to this.document. ToString () + 'On rank' + this.rank;} Working posting document () {Return Object.getPrototypeOf (this);} Posting function (query, rank) {this.query = query; This.rank = rank; This. Document = posting document; This.toString = PostingStostring;} Posting Posting (query, document, rank) {var posting = new posting (query, rank); // Prototype date here Is used for posting. Prototype = document; returning posting;}    

No comments:

Post a Comment