Il existe de nombreux articles, des guides pour apprendre JavaScript en général et les techniques pour l'utilisation de JavaScript effecitvely fil des ans.
- Demo
- Agrandir
- Recharger
- New window
G�n�rez vos vid�os d'entreprise par l'IA avec la voix ou simplement du texte
Votre premi�re application vid�o AI GRATUITE ! Automatisez votre premi�re vid�o AI. Cr�ez votre vid�o professionnelle en 5 minutes gr�ce � l'IA. Aucun �quipement ni comp�tence en montage vid�o requis. Production vid�o sans effort pour les sp�cialistes du marketing de contenu.
4. Using Namespaces to Prevent Conflicts
If you're doing an extensive amount of raw JavaScript coding and suspect that additions could be made to the same pages you're working on, you can prevent any future conflicts with your code by giving your code its own namespace.
Object-oriented JavaScript implements namespace-like principles due to the fact that properties and methods are declared inside of objects, thus there are less likely to be conflicts. A conflict could arise, however, through object names. And very likely, the conflict will occur "silently", thus you may not be alerted to the issue immediately.
You can prevent all conflicts by creating a unique namespace. Let's use the showStatistics
function to demonstrate how we can encapsulate code into its own namespace:
if (typeof MY == "undefined") { MY = new Object(); MY.CUSTOM = new Object(); } MY.CUSTOM.namespace = function() { function showStatistics(args) { document.write("<p><strong>Name:</strong> " + args.name + "<br />"); document.write("<strong>Team:</strong> " + args.team + "<br />"); if (typeof args.position === "string") { document.write("<strong>Position:</strong> " + args.position + "<br />"); } if (typeof args.average === "number") { document.write("<strong>Average:</strong> " + args.average + "<br />"); } if (typeof args.homeruns === "number") { document.write("<strong>Home Runs:</strong> " + args.homeruns + "<br />"); } if (typeof args.rbi === "number") { document.write("<strong>Runs Batted In:</strong> " + args.rbi + "</p>"); } } showStatistics({ name: "Mark Teixeira", team: "New York Yankees", position: "1st Base", average: .284, homeruns: 32, rbi: 101 }); } MY.CUSTOM.namespace();
The first few lines create the namespace by checking to see if the "MY
"
object already exists. This object can be whatever you want it to be.
Just pick a name that you don't think will ever be used again. After
the MY
object is created, we are then able to create the "CUSTOM
" object as a property of the MY
object. Then our namespace
function becomes a method of the MY.CUSTOM
object. Keep in mind that "MY
", "CUSTOM
" and "namespace
" can each be your own custom names. I chose these for demonstration purposes. They could be CHEESEBURGER.ONIONS.pickles
if you want!
The showStatistics
function is exactly the same as in
the example earlier that utilizes an object literal to pass in the
values. But in this case, the entire function, including the object
literal, is encapsulated inside my.custom.namespace
. The
last line invokes the entire function using dot notation, and the
function runs exactly the same as it normally would, except that it is
protected from conflicting with another function called "showStatistics
".
Further Reading:
5. Hybrid Application Development
You can create powerful JavaScript applications if you use a combination of a JavaScript library and raw JavaScript code. Many JavaScript libraries are used to implement "pretty" animations and other customizable effects-sometimes via plugins- that often don't require much to be added to them other than some custom values.
On the other hand, there may be situations where you'll want to accomplish something specificly requested by a client. Maybe it's something not available in a library and that requires extensive coding, possibly utilizing Ajax and a variety of DOM methods.
There is no point in reinventing the wheel. You can implement your favorite JavaScript library and take advantage of its simplified Ajax calls, DOM methods, and normalization of browser differences. Thus, you can have the advantages of the library, while still creating custom scripts that are specific to your project.
Further Reading:
- List of JavaScript libraries at Wikipedia
- 40 Useful JavaScript Libraries (Smashing Magazine)
- JavaScript Libraries: A directory of tools shaping the new web
6. Rendering Readable HTML
Finally, this is a technique to use in situations that require dozens of lines of HTML code being generated dynamically via JavaScript. Take the following example:
var pageContainer = document.getElementById("container"); var pageTitle = "Content Title"; var authorBio = "Mr. Lorum Ipsum"; var pageContent = "Lorum ipsum line text here. Lorum ipsum line text here. Lorum ipsum line text here. Lorum ipsum line text here. Lorum ipsum line text here. Lorum ipsum line text here. Lorum ipsum line text here. Lorum ipsum line text here. Lorum ipsum line text here."; var footerContent = "Copyright 2009"; var HTMLCode = '\n<h1>' + pageTitle + '</h1>\n <div id="content">\n <p>' + pageContent + '</p>\n <div id="author_bio">\n <p>' + authorBio +'</p>\n </div>\n </div>\n <div id="footer"> <p>' + footerContent + '</p>\n </div>\n'; pageContainer.innerHTML = HTMLCode;
The line to take note of above is the one that declares the value of the HTMLCode variable. It renders just find in the generated source code, since it utilizes the "new line" character, so it looks like perfectly good HTML. But if this line of code were any longer it would be extremely difficult to read and maintain in the .js file.
Here is the same code as above, but implementing a much more organized method of displaying the HTML:
var pageContainer = document.getElementById("container"); var pageTitle = "Content Title"; var authorBio = "Mr. Lorum Ipsum"; var pageContent = "Lorum ipsum line text here. Lorum ipsum line text here. Lorum ipsum line text here. Lorum ipsum line text here. Lorum ipsum line text here. Lorum ipsum line text here. Lorum ipsum line text here. Lorum ipsum line text here. Lorum ipsum line text here."; var HTMLCode = '\n' + '<h1>' + pageTitle + '</h1>\n' '<div id="content">\n' + '<p>' + pageContent + '</p>\n' + '<div id="author_bio">\n' + '<p>' + authorBio + '</p>\n' + '</div>\n' '</div>\n' + '<div id="footer">' + '<p>' + footerContent + '</p>\n' + '</div>\n'; pageContainer.innerHTML = HTMLCode;
Now the code is much more readable, and conforms to the manner in which HTML is rendered in an actual HTML page. It even includes proper HTML indenting, and still uses the new line character to properly format the outputted HTML.
Conclusion
Although I didn't provide a detailed explanation of every concept dealt with in this collection, I hope this list provided beginning and intermediate JavaScript coders with an overview of a few fairly advanced practical techniques that they can implement in future projects or experiments.
Please feel free to comment on any of the techniques I've mentioned and some specific ways that you have used them in your own applications.
- 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
Artificial General Intelligence
Ai and higher level Artificial General Intelligence (AGI)
Artificial General Intelligence
Ai and higher level Artificial General Intelligence (AGI)
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
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
Powerful AI Presentation PPT Maker for FREE
Build an impressive presentation with our free online AI presentation app
Your next top AI Assistant
Claude AI, developed by Anthropic
Your next top AI Assistant
Claude AI, developed by Anthropic
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 $