已弃用的替代__proto__

时间:2011-09-22 05:08:51

标签: javascript

当然,我是一个javascript noob(充其量)。以下代码似乎工作正常。任何想法如何保持相同的“初始化”方法并使其工作而不使用__proto__并且不将所有内容转换为构造函数?

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

我确实有一种方法可行,但我想知道是否有更有效的方法。现在,我所做的就是用这样调用我自己的继承函数来替换引用__proto__的行。

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

这是我的inherit()函数

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

3 个答案:

答案 0 :(得分:30)

Object.getPrototypeOf

// old-way
obj.__proto__

// new-way
Object.getPrototypeOf(obj)

答案 1 :(得分:9)

为什么你不使用标准的javascript函数继承?例如:

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*/);

答案 2 :(得分:0)

__proto__将在ES6中,所以也许如果你现在正在读这篇文章,你不应该需要这个但是它仍然很好知道