2011-09-22 13 views
14

के लिए विकल्प मान्य है कि मैं एक जावास्क्रिप्ट नोब (सर्वोत्तम रूप से) हूं। निम्नलिखित कोड ठीक काम करता प्रतीत होता है। कोई भी विचार कैसे "प्रारंभकर्ता" दृष्टिकोण को बनाए रखने और इसे __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(); 

मेरे पास एक दृष्टिकोण है जो काम करता है, लेकिन मुझे आश्चर्य है कि कोई और अधिक कुशल दृष्टिकोण है या नहीं। अभी के लिए, मैंने जो किया है वह उन पंक्तियों को प्रतिस्थापित करता है जो __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

यह जानना अच्छा है आपकी मदद कर सकता है: http://www.webdeveasy.com/javascript-prototype/ – Naor

उत्तर

8

आप का उपयोग मानक जावास्क्रिप्ट समारोह विरासत क्यों न? उदाहरण के लिए:

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

ठीक है मुझे लगता है कि "नोब" मेरी सोच में महत्वपूर्ण थी। मैंने सोचा कि मैं एक और जेएसओएन अनुकूल दृष्टिकोण ले रहा था लेकिन स्पष्ट रूप से आप जो सुझाव देते हैं वह सबसे अच्छा है। मैं एक उद्देश्य-सी पृष्ठभूमि से आया हूं, इसलिए मुझे वास्तव में ऑब्जेक्ट उन्मुख व्यवहार करने के लिए इस प्रोटोटाइप भाषा को प्राप्त करने में कठिनाई हो रही है। धन्यवाद –

0

__proto__, इसलिए हो सकता है अगर आप अब यह पढ़ रहे ES6 में हो जाएगा, तो आप इस आवश्यकता नहीं होनी चाहिए लेकिन यह अभी भी

+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

संबंधित मुद्दे