2008-12-10 11 views
18

मैं जावास्क्रिप्ट में कक्षाओं (और नामस्थान) का अनुकरण कैसे कर सकता हूं?मैं जावास्क्रिप्ट में "कक्षाएं" का अनुकरण कैसे कर सकता हूं? (किसी तृतीय-पक्ष लाइब्रेरी के साथ या उसके बिना)

मुझे एक जावास्क्रिप्ट लाइब्रेरी बनाने की आवश्यकता है और भाषा के साथ सीमित अनुभव है। मैंने हमेशा सोचा कि कक्षाओं के लिए इसका मूल समर्थन था, लेकिन यह जावा से कम है जो मैंने ग्रहण किया था। ऐसा लगता है कि जावास्क्रिप्ट में सब कुछ वास्तव में एक समारोह है।

जो मैंने अभी तक पाया है, वह गतिशील कमजोर भाषा वाली भाषा के साथ बहुत समझ में आता है, लेकिन इससे लोगों के लिए प्रस्थान का थोड़ा सा हिस्सा बनता है जिनका उपयोग दृढ़ता से टाइप की गई भाषा और एक कंपाइलर का उपयोग करने के लिए किया जाता है हमारी त्रुटियों को स्पॉट करें :)

मैं मुख्य रूप से सी # और जावा में काम करता हूं, और कुछ सिंटैक्टिकल समानता की उम्मीद कर रहा था ताकि पुस्तकालय हमारे अन्य सी # डेवलपर्स के लिए परिचित लगेगा जिन्हें इसे बनाए रखना होगा।

मेरे पास निम्न प्रकार का कोड है जो काम करता है, लेकिन मैं सोच रहा था कि इस पर अन्य डेवलपर का क्या होगा। विकल्प क्या हैं? क्या कोई रास्ता बेहतर है? क्या कोई तरीका है जो अधिक पठनीय है?

मैं समझता हूँ कि क्या मैं चाहता हूँ सी # या जावा के लिए कुछ इसी तरह जब मैं सिर्फ तथ्य यह है कि इस , जावास्क्रिप्ट है स्वीकार करना चाहिए है, लेकिन मेरा उद्देश्य यह अधिक परिचित बनाकर अन्य डेवलपर्स के लिए सीखने की अवस्था को कम करने की कोशिश करने के लिए है और उनके लिए सहज ज्ञान युक्त।

//Perform the namespace setup (this will actually be done with checking 
//in real life so we don't overwrite definitions, but this is kept short 
//for the code sample). 
var DoctaJonez = new function(); 
DoctaJonez.Namespace1 = new function(); 

/** 
* Class description. 
*/ 
DoctaJonez.Namespace1.SomeClass = function() 
{ 
    /** 
    * Public functions listed at the top to act like a "header". 
    */ 
    this.publicFunction = privateFunction; 

    /** 
    * Private variables next. 
    */ 
    var privateVariable; 

    /** 
    * Finally private functions. 
    */ 
    function privateFunction() 
    { 

    } 
} 

//Create an instance of the class 
var myClass = new DoctaJonez.SomeClass(); 

//Do some stuff with the instance 
myClass.publicFunction(); 

उत्तर

1

उदाहरण के लिए, http://mckoss.com/jscript/object.htm में।

"जावास्क्रिप्ट ऑब्जेक्ट उन्मुख" के लिए googling के दौरान आपको उदाहरणों का एक समूह मिल जाएगा। लोकप्रिय जावास्क्रिप्ट ढांचे जैसे Ext JS पर एक नज़र डालने से आपको इन अवधारणाओं और अभ्यास में उन्हें कैसे लागू किया जा रहा है, इसके लिए एक भावना महसूस होगी।

1

mepcotterell अच्छे लिंक प्रदान करता है, लेकिन मैं यह जोड़ना चाहता हूं कि मैं व्यक्तिगत रूप से नामस्थानों को वस्तुओं के रूप में देखना पसंद करता हूं i.e. DoctaJonez.Namespace1 = {};

+0

मुझे नहीं पता था कि आप ऐसा कर सकते हैं, जो कि बहुत अधिक सुरुचिपूर्ण दिखता है। इनपुट के लिए धन्यवाद :) +1 –

9

OOP के सामान्य समझ जावास्क्रिप्ट में आप से Douglas Crockford पढ़ बेहतर नहीं कर सकते के लिए:

डोजो प्रशंसकों के लिए

(और सामान्य तकनीक के लिए) Neil Roberts में अच्छे लेख हैं:

सादे वेनिला dojo.declare() शायद चारों ओर मुख्यधारा पुस्तकालयों में सबसे उन्नत OOP आधार है। मैं पक्षपातपूर्ण हूं, लेकिन इसके लिए मेरा शब्द न लें।इसका उपयोग कैसे करें इसके उदाहरण यहां दिए गए हैं।

एक सादे वेनिला वस्तु:

// Let's define a super simple class (doesn't inherit anything). 
dojo.declare("Person", null, { 
    // Class-level property 
    answer: 42, 

    // Class-level object property 
    name: {first: "Ford", last: "Prefect"}, 

    // The constructor, duh! 
    constructor: function(age){ 
    this.age = age; // instance-level property 
    }, 

    // A method 
    saySomething: function(verb){ 
    console.log("I " + verb + " " + 
     this.name.first + " " + this.name.last + "!" + 
     " -- " + this.answer); 
    }, 

    // Another method 
    passportControl: function(){ 
    console.log("I am " + this.age); 
    } 
}); 

उपयोग का उदाहरण:

// A fan of Ford Perfect 
var fan = new Person(18); 
fan.saySomething("love"); // I love Ford Perfect! -- 42 
fan.passportControl(); // I am 18 

एकल विरासत आसान है:

// Let's create a derived class inheriting Person 
dojo.declare("SuperAgent", Person, { 
    // Redefine class-level property 
    answer: "shaken, not stirred", 

    // Redefine class-level object property 
    name: {first: "James", last: "Bond"}, 

    // The constructor 
    constructor: function(age, drink){ 
    // We don't need to call the super class because 
    // it would be done automatically for us passing 
    // all arguments to it. 

    // At this point "age" is already assigned. 

    this.drink = drink; // Instance-level property 
    }, 

    // Let's redefine the method 
    saySomething: function(verb){ 
    // Let's call the super class first 
    this.inherited(arguments); 
    // Pay attention: no need for extra parameters, or any extra code, 
    // we don't even name the class we call --- it is all automatic. 
    // We can call it any time in the body of redefined method 

    console.log("Yeah, baby!"); 
    }, 

    shoot: function(){ console.log("BAM!!!"); } 
}); 

उपयोग का उदाहरण:

// Let's create a James Bond-wannabe 
var jb007 = new SuperAgent(45, "Martini"); 
jb007.saySomething("dig"); // I dig James Bond! -- shaken, not stirred 
          // Yeah, baby! 
jb007.passportControl(); // I am 45 
jb007.shoot();    // BAM!!! 

// Constructors were called in this order: Person, SuperAgent 
// saySomething() came from SuperAgent, which called Person 
// passportControl() came from Person 
// shoot() came from SuperAgent. 

mixins:

// Let's define one more super simple class 
dojo.define("SharpShooter", null, { 
    // For simplicity no constructor 

    // One method to clash with SuperAgent 
    shoot: function(){ 
    console.log("It's jammed! Shoot!"); 
    } 
}); 

Mixin आधारित एकाधिक वंशानुक्रम:

// Multiple inheritance 
dojo.declare("FakeAgent", ["SuperAgent", "SharpShooter"], { 
    // Let's do it with no constructor 

    // Redefine the method 
    saySomething: function(verb){ 
    // We don't call super here --- a complete redefinition 

    console.log("What is " + verb "? I want my " + this.drink + "!"); 
    }, 
}); 

उपयोग का उदाहरण:

// A fake agent coming up 
var ap = new FakeAgent(40, "Kool-Aid"); 
ap.saySomething("hate"); // What is hate? I want my Kool-Aid! 
ap.passportControl(); // I am 40 
ap.shoot();    // It's jammed! Shoot! 

// Constructors were called in this order: Person, SuperAgent 
// saySomething() came from FakeAgent 
// passportControl() came from Person 
// shoot() came from SharpShooter. 

आप देख सकते हैं, dojo.declare() एक साधारण के साथ सभी आवश्यकताओं देता है उपयोग करने के लिए एपीआई: सीधे एकल विरासत, मिश्रण-आधारित एकाधिक विरासत, रचनाकारों की स्वचालित श्रृंखला, और कोई परेशानी सुपर विधियां नहीं।

1

यदि आप (और आपकी टीम) जावा पर उपयोग किए जाते हैं लेकिन किसी वेबसाइट के लिए कुछ जावास्क्रिप्ट बनाने की आवश्यकता है, तो आपको Google Web Toolkit (GWT) पर विचार करना चाहिए। यह आपको Java का उपयोग करके जावास्क्रिप्ट को कोड करने देता है, जिसे जावास्क्रिप्ट में परिवर्तित किया जाता है। मैंने कोशिश नहीं की है, यद्यपि।

जावास्क्रिप्ट वास्तव में एक बहुत अच्छी भाषा है। इसमें कुछ त्रुटियां हैं (आपको बहुत बेवकूफ सामान करने की अनुमति भी शामिल है), लेकिन थोड़ी सी आत्मनिर्भरता के साथ आप बहुत अच्छी चीजें बना सकते हैं। जावास्क्रिप्ट वास्तव में ऑब्जेक्ट -oriented, इतना कक्षा-केंद्रित नहीं है, लेकिन आप एक ही सामान कर सकते हैं। आपके पास (AFAIK) विरासत नहीं है, लेकिन यह टाइपिंग के साथ बिल्कुल सख्त नहीं है (इसकी शक्तिशाली लेकिन खतरनाक विशेषताओं में से एक) ताकि आप पाएंगे कि यह सीमित नहीं है।

+0

यह बहुत दिलचस्प लगता है। मैं आगे की जांच करूंगा, लिंक के लिए धन्यवाद :) –

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

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