Programmation Orientée Objet (POO) - une tendance de la programmation de codage est la cause de plus en plus populaire de ses avantages.
- Demo
- Agrandir
- Recharger
- New window
Gratuit iPage h�bergement Web pour la premi�re ann�e MOMENT
Si vous �tes toujours � la recherche d'un fournisseur d'h�bergement Web fiable avec des tarifs abordables, pourquoi vous ne prenez pas un peu de temps pour essayer iPage, seulement avec $1.89/month, inclus $500+ Cr�dits suppl�mentaires gratuites pour le paiement de 24 mois ($45)?
Plus de 1.000.000 de clients + existisng peuvent pas avoir tort, vraiment vous n'�tes pas aussi! Plus important encore, lorsque vous enregistrez l'h�bergement web � iPage gr�ce � notre lien, nous allons �tre heureux de renvoyer un plein remboursement. C'est g�nial! Vous devriez essayer iPage h�bergement web GRATUITEMENT maintenant! Et contactez-nous pour tout ce que vous devez savoir sur iPage.
For all those that use Object Oriented Programming know the benefits. The ability to write reusable code is a real time saver in the long run. This post will cover how to write the basic structure of Object Oriented Programming in JavaScript.
The Basics
For those of you that don't know what Object Oriented Programming (oop) is, it's simply a way of writing code that allows you to reuse the same code in several other projects. Everything is encapsulated into nice little packages. This post won't go over the actual theory of OOP, but I would definitely recommend looking into it. I'm sure if you Googled it, thousands of entries would come up.
The Ways
There are a couple of ways to handle creating a class in JavaScript.
- Placing everything inside a function
- Placing everything inside an object
- Using prototype to build a class
Placing Everything Inside a Function
In my opinion this is probably the easiest to read out of all of them. However it isn't the most efficient from a processor point of view.
Say were going to build a Dog class.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | function Dog () { this.name = ""; this.position = 0; this.bark = function (){ alert("wuff"); }; this.walk = function(){ this.position += 1; alert("position = "+this.position); } this.getName = function (){ return this.name; } } var dog = new Dog(); dog.name = "Ralph"; dog.bark(); //Popup box with "wuff" dog.walk(); //Position increases by 1; alert(dog.getName()); //Outputs dog's name*/ |
Class methods are simply variables assigned to functions, and public
variables simply use "this" infront of their names. If you wish to make
a private variable, you would just create a regular variable
using "var" (e.g. var test = 1;)
Placing Everything Inside an Object
This is handy if you don't want to instantiate the class every time you want to use it. The downside is that it makes inheritance a little more difficult.
Here is the same Dog class but using an Object.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | var Dog = { name : "", position: 0, bark : function(){ alert("wuff") }, walk : function(){ this.position += 1; alert("position = "+this.position); }, getName : function(){ return this.name } } Dog.name = "Ralph"; Dog.bark(); //Popup box with "wuff" Dog.walk(); //Position increases by 1; alert(Dog.getName()); //Outputs dog's name |
This method uses JavaScript Object Notation (JSON) to layout the class. Keys go on the left, and values go on the right. Once again methods are simply functions assigned to variables.
Using prototype to Build a Class
This is probably the preferred method of OOP in JavaScript. It's a little harder to read, but say if we were to create thousands of dog objects, this would out perform the other methods. Using prototypes it doesn't store the functions within the class. This means you aren't creating thousands of functions, you're only create one function that is pointed to a thousand times. Plus it's easier to deal with inheritance.
Here is the Dog class yet again.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | function Dog (){} Dog.prototype.name = ""; Dog.prototype.position = 0; Dog.prototype.bark = function (){ alert("wuff"); } Dog.prototype.walk = function (){ this.position += 1; alert("position = "+this.position); } Dog.prototype.getName = function(){ return this.name; } var dog = new Dog(); dog.name = "Ralph"; dog.bark(); //Popup box with "wuff" dog.walk(); //Position increases by 1; alert(dog.getName()); //Outputs dog's name |
What the prototype keyword is doing here is creating a framework. So when the class is instanciated it will have all the methods and properties that it needs.
Here is a Puppy class to show how inheritance is done. It inherits all the methods of the Dog class, but overwrites the bark method for a more puppy like bark.
1 2 3 4 5 6 7 8 9 10 11 12 13 | function Puppy (){} Puppy.prototype = new Dog(); Puppy.prototype.bark = function (){ alert("Yelp"); } var puppy = new Puppy(); puppy.name = "Ralph"; puppy.bark(); //Popup box with "Yelp" puppy.walk(); //Position increases by 1; alert(puppy.getName()); //Outputs puppy's name |
Conclusion
This is by no means a complete tutorial of everything OOP. This shows the basic structures of how JavaScript can handle OOP. Out of the three that were outlined, it is probably best to stick with the prototype method. If there are any Actionscript 2 people out there, you'll feel right at home with this method.
- Sent (0)
- Nouveau
Générez vos vidéos d'entreprise par l'IA avec la voix ou simplement du texte
chatGPTaz.com
Parlez à ChatGPT dans votre langue maternelle
AppAIVidéo
Votre première application vidéo AI GRATUITE
Deepfake Video
Deepfake AI Video Maker
Deepfake
Deepfake AI Video Maker
AI Deep Fake
Deepfake AI Video Maker
AIvidio
AI Video Mobile Solutions
AIvideos
AI Video Platform & Solutions
AIvedio
AI Video App Maker
Faceswap AI en ligne
Échangez des visages, des vidéos, des photos et des GIF instantanément avec de puissants outils d'IA - Faceswap AI Online GRATUIT
Faceswap AI en ligne
Échangez des visages, des vidéos, des photos et des GIF instantanément avec de puissants outils d'IA - Faceswap AI Online GRATUIT
Temu gratuit 500 $ pour les nouveaux utilisateurs
Claim Free Temu $500 Credit via Affiliate & Influencer Program
Crédits publicitaires TikTok gratuits
Maîtrisez les publicités TikTok pour le marketing de votre entreprise
Dall-E-OpenAI.com
Générez automatiquement des images créatives avec l'IA
chatGPT4.win
Parlez à ChatGPT dans votre langue maternelle
Premier produit d'intelligence artificielle d'Elon Musk - Grok/UN.com
Parlez au chatbot Grok AI dans votre langue
Outily.win
Centre d'outils ouvert et gratuit, utilisable par tous et pour tous, avec des centaines d'outils
GateIO.gomymobi.com
Airdrops gratuits à réclamer et à partager jusqu'à 150 000 $ par projet
iPhoneKer.com
Économisez jusqu'à 630 $ à l'achat d'un nouvel iPhone 16
Acheter le robot Tesla Optimus
Commandez votre robot Tesla Bot : Optimus Gen 2 dès aujourd'hui pour moins de 20 000 $
OOP R�ponse