Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I think you misunderstand prototypes a bit.</p> <p>Given a function <code>Foo</code>, <code>Foo.prototype</code> is not the prototype of the <code>Foo</code> object. It is the prototype that will be assigned to objects created using <code>new Foo()</code>. For example:</p> <pre><code>// This is a constructor that creates objects whose prototype is Person.prototype var Person = function(name) { this.name = name; } Person.prototype.sayHello = function() { console.log("Hello, my name is " + this.name); } var drew = new Person('Drew'); drew.sayHello(); // &lt;-- Logs a message drew.__proto__; // &lt;-- Not part of the Javascript spec, but it some browsers this is a reference to Person.prototype </code></pre> <p>Your main.get.registration could be implemented without prototypes:</p> <pre><code>main = function() {/* do stuff*/} main.get = function() {/* this is a getter function? */} main.get.registration = function() {/* I don't know what this does */} </code></pre> <p>What kind of interface or API are you hoping to create? Does it involve creating objects using <code>new</code>?</p> <p><strong>UPDATE:</strong> Here's one of many possible ways to implement what you want:</p> <pre><code>main = function() { // store a reference to this instance. var self = this; // Construct the get object. It doesn't need to be a function because it's never invoked this.get = {}; this.get.registration = function() { // Use self to refer to the specific instance of main you're interacting with. retrieveRegistrationFor(self); // &lt;-- pseudo-code } } </code></pre> <p><strong>UPDATE 2:</strong> Here's how to construct the <code>get</code> object using a constructor, allowing you to use prototypes for everything. I've capitalized the names of your constructors, which is a best practice that helps to differentiate between normal functions/methods and constructors.</p> <pre><code>// Constructor for the get object. This is only ever invoked in Main's constructor. Getter = function(mainInstance) { this.self = mainInstance; } Getter.prototype.registration = function() { retrieveRegistrationFor(this.self); // &lt;-- pseudo-code } Main = function() { // Construct the get object and attach it to this object. this.get = new Getter(this); } </code></pre> <p>As the other answers have pointed out, there are lots of ways to construct objects in Javascript. It all depends on the situation and your personal coding style.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload