Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Expose prototypes in object

Is there a way to expose an objects' prototypes through another object?

  var foo = function () {
    var foo = {
      bar: bar,
      boo: boo
    }
    return foo;

    function bar (age) {
      this.age = age;
    }
    bar.prototype.shoutAge = function () {
      alert('My age is ' + this.age);
    }

    function boo (age) {
      this.age = age;
      boo.prototype.shoutAge = function () {
        alert('My age is ' + this.age);
      }
    }
  }

  var foo = foo();
  var far = new foo.bar(13); // bar {age: 13}
  far.shoutAge(); // far.shoutAge is not a function

  var far = new foo.boo(13); // boo {age: 13} with __proto__
  far.shoutAge(); // alert('My age is 13'); 

In this code, the 'bar' object is set up with prototype inheritance--but exposing bar loses the inheritance of 'shoutAge'.

However, the 'boo' object has the prototype declared inside of it and the outside function has access to the method 'shoutAge'.

I don't think that the latter is a best practice even though it functions. So what would the best practice here be?

I don't want each instance of 'bar' to have it's own 'shoutAge' method, especially if there are potentially hundreds of instances of 'bar'. Would you typically just create a separate object for 'bar' instead of exposing it through 'foo'?

like image 752
amarshall Avatar asked Jul 01 '16 18:07

amarshall


People also ask

How do I access prototype objects?

Its name is not standard, but in practice all browsers use __proto__ . The standard way to access an object's prototype is the Object. getPrototypeOf() method. When you try to access a property of an object: if the property can't be found in the object itself, the prototype is searched for the property.

What is the prototype of an object?

The prototype of an object is referred to by the prototype property of the constructor function that creates and initializes the object. The isPrototypeOf() method provides a way to determine if one object is the prototype of another. This technique can be used to determine the class of an object.

How do you set an object prototype?

The Object. setPrototypeOf() method sets the prototype (i.e., the internal [[Prototype]] property) of a specified object to another object or null. All JavaScript objects inherit properties and methods from a prototype. It is generally considered the proper way to set the prototype of an object.

What is object __ proto __?

The __proto__ getter function exposes the value of the internal [[Prototype]] of an object. For objects created using an object literal, this value is Object. prototype . For objects created using array literals, this value is Array.


1 Answers

Looks like this is just an order-of-operations issue:

var Foo = function () {

  function bar (age) {
    this.age = age;
  }
  bar.prototype.shoutAge = function () {
    alert('My age is ' + this.age);
  }

  var foo = {
    bar: bar,
    boo: boo
  }
  return foo;
}

I think this is because the bar.prototype assignment is never executed in your code. The bar function definition gets hoisted, so it's available in the object you return, but the prototype assignment is an additional statement after the return statement, and the control flow never reaches it. Simply moving the return to the end fixes the issue.

like image 98
nrabinowitz Avatar answered Oct 23 '22 12:10

nrabinowitz