Ce JavaScript tutorial discuter de l'explication sommaire et les exemples simples JavaScript de domaines et de contextes au JavaScript Object Oriented Programming (OOP).
- 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.
[Update: thanks to Howard, I've updated the typo regarding global functions.]
In a previous post, we've talked about scopes. Today we'll be talking about execution contexts. Every line of JS code you write runs in a certain execution context. If you're coming to JS from an object oriented language, then you should recognize this concept. In Javascript, you can use the this keyword to access the current execution context. Even though it's not the most precise way defining it, you can think of this as referencing the object that the code is currently inside of. In practice, this will always return a valid reference.
You probably recall our previous post on scopes. At the time, I've said that global scope variables end up being added to the window object. This happens because window represents the default execution context at that scope. We need an example to better understand execution contexts. Take a look at the following snippet:
//current execution context: window var globalMsg = "Hello"; function sayHi() { //new scope: globalMsg is local //but this still points to global execution context var globalMsg = "Inside hello"; alert(globalMsg + "-" + this.globalMsg); } var obj = { globalMsg: "Hallo!", sayHi: function() { //new scope //notice that execution context is the object //itself and not window alert(this.globalMsg); } }; sayHi(); obj.sayHi();
The previous snippet "mixes" scopes and execution contexts. We start by declaring a global variable and we initialize it to "Hello". We then create a global function (sayHi). As you know by now, functions introduce new scopes. However, they don't influence the "current" execution context. This leads to two conclusions:
- inside the global sayHi, the globalMsg identifier will always reference the "local" variable declared within the function body (because that function introduced a new scope and that means that "local" variables declarations hide global variables);
- the this reference still references the default context, ie, this still references the global window object (notice that I'm assuming that we're talking about an HTML page).
If you understand the previous observations, then you won't be surprise to see that executing the global function sayHi results in showing an alert with the text "Inside Hello-Hello".
obj is also a "global" variable which has been "added" to the window object. In the previous snippet, obj references an anonymous object which has a property and a function. The most important thing you should take from the code is that when you execute the obj.sayHi() call, you're introducing a "new" execution context within the obj.sayHi function: in this case, this references the current object instead of the global window object. That's why that function call will show an alert message with the "Hallo!".
Javascript allows us to set up the execution context of a function (that is, JS allows us to change the value of the this reference that gets passed to the executing function). To do that, we need to use the apply or the call methods of the function object (the main difference between the two rely on the way we pass parameters to the function - I'll probably be back to this topic in a future post)
Lets translate our knowledge on execution contexts to write a function that changes the background color of any element. We can reuse our knowledge of execution contexts to do that. Before we write the code, let's add some HTML to the test page:
<p id="paragraph">This is a paragraph</p> <div id="div">This is a div</div>
And now, we can write the global function that changes the background color:
function changeColor(color) { this.style.backgroundColor = color; }
Ok, now we're only missing one thing: how to influence the execution context of the function and make it reference the adequate element. If we call the function, this will reference the window object (and you'll end up with an error because window doesn't have a style property). That's why we need to use the function's apply method. Here's the code for changing the background color of the paragraph to red:
changeColor.apply( document.getElementById("paragraph"), //set this reference ["red"] //array of parameters );
the first parameter establishes the execution context (ie, the this reference) inside the changeColor function. Notice that apply expects an array with the values that will be passed to the function during the "current" execution.
Understanding execution contexts and how to influence them is really important. This feature is heavily used by all JS platforms. For instance, JQuery uses it a lot in several places so that this points to the "current logic" element. And now you know how you can take advantage of this feature in your code too. Keep tuned for more on Javascript.
- 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 $