Note that there are some explanatory texts on larger screens.

plurals
  1. POalternative for the deprecated __proto__
    primarykey
    data
    text
    <p>Granted I'm a javascript noob (at best). The following code seems to work fine. Any ideas how to keep the same "initializer" approach and make it work without using <code>__proto__</code> and without converting everything to constructor functions?</p> <pre><code>var Employee = { paygrade: 1, name: "", dept: "general", init: function() { return this; }, salary: function() { return this.paygrade * 30000; } }; var WorkerBee = { paygrade: 2, projects: ["Project1", "Project2"], init: function() { this.__proto__ = Inherit_Employee; // Inherit My Employee "Pseudo Prototype" return this; } }; var SalesPerson = { dept: "Sales", quota: 100, init: function() { this.__proto__ = Inherit_WorkerBee; // Inherit My WorkerBee "Pseudo Prototype" return this; } }; var Engineer = { dept: "Engineering", machine: "im the start machine", init: function() { this.__proto__ = Inherit_WorkerBee; // Inherit My WorkerBee "Pseudo Prototype" return this; } }; var Inherit_Employee = Object.create(Employee).init(); // Create My Employee Pseudo-Prototype var Inherit_WorkerBee = Object.create(WorkerBee).init(); // Create My WorkerBee Pseudo-Prototype var jane = Object.create(Engineer).init(); var jill = Object.create(Engineer).init(); </code></pre> <p>I do have one approach that works, but I'm wondering if there is a more efficient approach. For now, what I have done is replace the lines that reference <code>__proto__</code> with a call to my own inheritence function like this.</p> <pre><code> init: function() { inherit(this, WorkerBee); // Inherit WorkerBee return this; } </code></pre> <p>And this is my inherit() function</p> <pre><code>function inherit( childObject, parentObject ) { // childObject inherits all of parentObjects properties // for (var attrname in parentObject) if ( childObject[attrname] == undefined ) childObject[attrname] = parentObject[attrname]; // childObject runs parentObject 'init' function on itself // for (var attrname in parentObject) if ( typeof parentObject[attrname] == "function" ) if ( attrname == 'init' ) parentObject[attrname].call(childObject); } </code></pre>
    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.
 

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