2010-06-20 11 views
14

मैं ऑब्जेक्ट.क्रेट() के साथ कैसे उत्तराधिकारी हूं? मैं इन की कोशिश की, लेकिन कोई भी काम कर रहे हैं:Object.create() के साथ जावास्क्रिप्ट विरासत?

var B = function() {}; 
var A = function() {}; 
A = Object.create(B); 
A.prototype.C = function() {}; 

और

var B = function() {}; 
var A = function() {}; 
A.prototype.C = function() {}; 
A = Object.create(B); 

और

var B = function() {}; 
A = Object.create(B); 
var A = function() {}; 
A.prototype.C = function() {}; 

कुछ भी नहीं काम किया। मुझे इस नए ऑब्जेक्ट का उपयोग करना चाहिए .create() - फ़ंक्शन?

+0

[का उपयोग करना "Object.create" के बदले "नया"] के संभावित डुप्लिकेट (http://stackoverflow.com/questions/2709612/using-object-create-instead-of-new) – Bergi

उत्तर

21

Object.create() वस्तुओं का उत्तराधिकारी करने के लिए उपयोग किया जाता है, न कि आप जैसे रचनाकार करने की कोशिश कर रहे हैं। यह पुराने ऑब्जेक्ट सेट के साथ अपने प्रोटोटाइप माता-पिता के रूप में एक नई वस्तु बनाता है।

var A = function() { }; 
A.prototype.x = 10; 
A.prototype.say = function() { alert(this.x) }; 

var a = new A(); 
a.say(); //alerts 10 

var b = Object.create(a); 
b.say(); //alerts 10 
b.x = 'hello'; 
b.say(); //alerts 'hello' 

और बस यकीन है कि ख सिर्फ एक का क्लोन नहीं है बनाने के लिए,

a.x = 'goodbye'; 
delete b.x; 
b.say(); //alerts 'goodbye' 
+0

अरे, तो यह मेरी स्थिति में फिट नहीं है। मुझे एक "कक्षा" परिभाषित करने की आवश्यकता है जो एक और "कक्षा" फैली हुई है। – Tower

+0

प्रोटोटाइप विरासत का बिंदु यह है कि आपके पास "वर्ग" और "ऑब्जेक्ट" के बीच एक कठिन अंतर नहीं है क्योंकि कक्षा एक वस्तु है। – Kos

+0

मेरी इच्छा है कि 'ऑब्जेक्ट.क्लोन (ओबीजे)' था जो 'JSON.parse (JSON.stringify (obj)) के समान परिणाम उत्पन्न करेगा। – trusktr

0

डगलस ऑब्जेक्ट.क्रेट के लिए मूल दस्तावेज यहां http://javascript.crockford.com/prototypal.html है। सुनिश्चित करें कि आप विधि की परिभाषा

if (typeof Object.create !== 'function') { 
    Object.create = function (o) { 
     function F() {} 
     F.prototype = o; 
     return new F(); 
    }; 
} 
+0

.. हालांकि उस कार्यान्वयन का उपयोग करने की अनुशंसा नहीं की जाती है, देखें: http://stackoverflow.com/questions/5199126/javascript-object-create-not-working-in-firefox – zack

0

आप Object.create खुद को परिभाषित कर सकते, लेकिन अगर यह देशी नहीं है उसे अपने साथ पाश में आप वस्तुओं के लिए उपयोग के लिए हर में प्रगणित किया जा रहा निपटने के लिए होगा शामिल किया है ।

अभी तक केवल नए वेबकिट- सफारी 5 और क्रोम इसका मूल रूप से समर्थन करते हैं।

+3

'ऑब्जेक्ट.क्रेट' 'ऑब्जेक्ट 'की एक संपत्ति है, न कि 'ऑब्जेक्ट.प्रोटोटाइप' का, इसलिए इसे स्वयं परिभाषित करना इसे सभी ऑब्जेक्ट्स पर समेकित गुणों की सूची में नहीं जोड़ देगा। – kpozin

0

आप मोज़िला विकास केंद्र पर उपयोगी जानकारी के बारे में JavaScript inheritance पा सकते हैं।

16

पैटर्न मैं इस के लिए उपयोग करती हैं इसलिए की तरह एक मॉड्यूल में प्रत्येक प्रकार के रैप करने के लिए, और बेनकाब create और prototype गुण, है:

var Vehicle = (function(){ 
     var exports = {}; 
     exports.prototype = {}; 
     exports.prototype.init = function() { 
       this.mph = 5; 
     }; 
     exports.prototype.go = function() { 
       console.log("Going " + this.mph.toString() + " mph."); 
     }; 

     exports.create = function() { 
       var ret = Object.create(exports.prototype); 
       ret.init(); 
       return ret; 
     }; 

     return exports; 
})(); 

तो मैं तो जैसे व्युत्पन्न प्रकार का निर्माण कर सकते हैं:

var Car = (function() { 
     var exports = {}; 
     exports.prototype = Object.create(Vehicle.prototype); 
     exports.prototype.init = function() { 
       Vehicle.prototype.init.apply(this, arguments); 
       this.wheels = 4; 
     }; 

     exports.create = function() { 
       var ret = Object.create(exports.prototype); 
       ret.init(); 
       return ret; 
     }; 

     return exports; 

})(); 
इस पैटर्न के साथ

, प्रत्येक प्रकार का अपना create() फ़ंक्शन है।

+0

अनुवर्ती: यदि मैं नहीं करता हूं तो मुझे क्लास जैसी चीजों को बनाने की आवश्यकता महसूस होती है, या इन दिनों मैं कॉफ़ीस्क्रिप्ट का उपयोग करता हूं। –

26

वहाँ जावास्क्रिप्ट

निर्माण विरासत में भाग कर के कई तरीके हैं।

function Rectangle(length, width) { 
    this.length = length; 
    this.width = width; 
} 

Rectangle.prototype.getArea = function() { 
    return this.length * this.width; 
}; 

// inherits from Rectangle 
function Square(size) { 
    this.length = size; 
    this.width = size; 
} 

Square.prototype = Object.create(Rectangle.prototype); 

var rect = new Rectangle(6, 8); 
var square = new Square(10); 

console.log(rect.getArea());    // 48 
console.log(square.getArea());    // 100 
console.log(rect instanceof Rectangle);  // true 
console.log(rect instanceof Object);  // true 
console.log(square instanceof Square);  // true 
console.log(square instanceof Rectangle); // true 
console.log(square instanceof Object);  // true 

निर्माता चोरी: यदि आप महाप्रकार निर्माता कॉल करने के लिए की जरूरत नहीं है प्रयुक्त। खेतों में प्रयुक्त करता है, तो कॉल करने के लिए की जरूरत महाप्रकार निर्माता:

function Rectangle(length, width) { 
    this.length = length; 
    this.width = width; 
} 

Rectangle.prototype.getArea = function() { 
    return this.length * this.width; 
}; 

// inherits from Rectangle 
function Square(size) { 
    Rectangle.call(this, size, size); 
} 

Square.prototype = Object.create(Rectangle.prototype); 

var rect = new Rectangle(6, 8); 
var square = new Square(10); 

console.log(rect.getArea());    // 48 
console.log(square.getArea());    // 100 
console.log(rect instanceof Rectangle);  // true 
console.log(rect instanceof Object);  // true 
console.log(square instanceof Square);  // true 
console.log(square instanceof Rectangle); // true 
console.log(square instanceof Object);  // true 
+0

मैं Square.prototype.constructor = स्क्वायर देखता हूं; कहीं में, इसकी आवश्यकता नहीं है? – c0ming

0

खैर यह साल देर हो चुकी है, लेकिन किसी और को इस पर ठोकर के लिए। आप एफएफ और क्रोम में ऑब्जेक्ट.साइन का उपयोग कर सकते हैं।

इस उदाहरण में जब क्यूब को बनाने के साथ बनाया जा रहा है। फर्स्ट ऑब्जेक्ट.क्रेट (यह) ऑब्जेक्ट जेड प्रॉपर्टी के साथ ऑब्जेक्ट बनाता है, फिर ऑब्जेक्ट.साइन (ओबीजे, स्क्वायर.क्रेट (एक्स, वाई) के साथ) यह स्क्वायर.क्रेट को कॉल करेगा और वापस लौटाएगा और क्यूब में ओबज में संग्रहीत किया जाएगा ।

var Square = { 
     x: 0, 
     y: 0, 

     create: function(x,y) { 
      var obj = Object.create(this); 
      obj.x = x; 
      obj.y = y; 
      return obj; 
     } 
    }; 

var Cube = { 

     z: 0, 

     create:function(x,y,z) { 
      var obj = Object.create(this); 
      Object.assign(obj, Square.create(x,y)); // assign(target,sources...) 
      obj.z = z; 
      return obj; 
     } 
    }; 

// Your code 
var MyCube = Cube.create(20,30,40); 
console.log(MyCube);