2011-09-22 6 views
14

私はjavascriptのnoob(せいぜい)です。次のコードは正常に動作するようです。任意のアイデアをどのように同じ "初期化子"のアプローチを維持し、__proto__を使用せずに動作させ、コンストラクタ関数にすべてを変換せずに?廃止された__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(); 

私には1つのアプローチがありますが、もっと効率的なアプローチがあるのでしょうか?今のところ、私がやったことは、__proto__という行を、このような自分自身の継承関数の呼び出しで置き換えることです。

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

そして、この私の継承である()関数

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

これを知っていることはまだ良いことだので、多分、ES6になりますあなたを助けるかもしれない:http://www.webdeveasy.com/javascript-prototype/ – Naor

答えて

8

あなたは、標準の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*/); 
+0

まあ、私は "Noob"が私の思考の鍵であったと思う。私はもっ​​とJSONのフレンドリーなアプローチを取っていたと思っていましたが、あなたが提案するものがはっきりと分かります。私はObjective-Cのバックグラウンドから来ているので、このプロトタイプ言語をオブジェクト指向の振る舞いにするのは苦労しています。ありがとう –

0

__proto__は、あなたが今、これを読んでいる場合は、あなたがこれを必要はありませんが、それは

+1

'__proto__'はES6では非推奨です。彼らは、従来の理由のためだけにその動作を指定しました。 – Bergi

24

Object.getPrototypeOf

// old-way 
obj.__proto__ 

// new-way 
Object.getPrototypeOf(obj) 
+0

.__ proto__ = bと同等のものは何ですか? –

+0

@GuidoGarcía:['Object.setPrototypeOf'](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/setPrototypeOf)(またはES6以降は実際には' Reflect.setPrototypeOf' ) – Bergi