开发者

alternative for the deprecated __proto__ [duplicate]

This question already has answers here: __proto__, when will it be gone? Alternatives? (3 answers) Closed 7 years ago.

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 __proto__ and without converting everything to constructor functions?

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();

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 __proto__ with a call to my own inheritence function like this.

    init: function()
        {
        inherit(this, WorkerBee);   // Inherit WorkerBee
        return this;
        }

And this is my inherit() function

function inherit( childObject, parentObject )
    {
    // childOb开发者_高级运维ject 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);
    }


Object.getPrototypeOf

// old-way
obj.__proto__

// new-way
Object.getPrototypeOf(obj)


Why you dont use standard javascript function inheritance? For example:

function inherit(childClass, parentClass) {
    var f = function() {}; // defining temp empty function
    f.prototype = parentClass.prototype;
    f.prototype.constructor = f;

    childClass.prototype = new f;

    childClass.prototype.constructor = childClass; // restoring proper constructor for child class
    parentClass.prototype.constructor = parentClass; // restoring proper constructor for parent class
}


Employee = function Employee( /*list of constructor parameters, if needed*/ ) {}
Employee.prototype.paygrade = 1;
Employee.prototype.name = "";
Employee.prototype.dept = "general";
Employee.prototype.salary = function() {
    return this.paygrade * 30000;
}


WorkerBee = function WorkerBee( /*list of constructor parameters, if needed*/ ) {
    this.projects = ["Project1", "Project2"];
}
inherit(WorkerBee, Employee); // for this implementation of *inherit* must be placed just after defining constructor
WorkerBee.prototype.paygrade = 2;
WorkerBee.prototype.projects = null; // only literals and function-methods can properly initialized for instances with prototype


Engineer = function Engineer( /*list of constructor parameters, if needed*/ ) {}
inherit(Engineer, WorkerBee);
Engineer.prototype.dept = "Programming";
Engineer.prototype.language = "Objective-C";

var jane = new Engineer( /*Engineer parameters if needed*/ );
var jill = new Engineer( /*Engineer parameters if needed*/ );
var cow = new Employee( /*Employee parameters if needed*/ );


__proto__ will be in ES6, so maybe if you're reading this now, you shouldn't need this but it's still good to know

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜