IF you often watch the HBO movies, then the title of this JavaScript post will be familiar to you: "Simply the Best". Yes, that's right, I want to borrow this slogan from HBO to describe the content of this post: it will provide you the very basic JavaScript concepts, and you will be able to access, understand the web programming JavaScript language easily and quickly.
This JavaScript article tutorial shows you the full-detailed practices, combined with live JavaScript example codes to try. At present, this JavaScript article tutorial us 5 parts and I'll update if there's new chapter, meanwhile, you can go through:
- Part 1: JavaScript Classes
- Part 2: JavaScript Inheritance
- Part 3: JavaScript and JSON
- Part 4: The Prototype Property
- Part 5: JavaScript Closures
Read more other JavaScript article tutorials on jsB@nk:
- JavaScript Function Declarations & JavaScript Function Expressions - Basic Concepts
- JavaScript Prototype: Some Basic Concepts
- 5 Chief JavaScript Inheritance Concepts
- Simple Concepts about Types and Objects in JavaScript OOP
- Top 10 Best JavaScript eBooks that Beginners should Learn
- Demo
- Enlarge
- Reload
- New window
Generate your business videos by AI with voice or just text
Your first FREE AI Video App! Automate Your First AI Video. Create Your Professional Video In 5 Minutes By AI No Equipment Or Video Editing Skill Requred. Effortless Video Production For Content Marketers.
Part 5: Javascript Closures
Closures are a tricky concept in Javascript. They seem deceptively simple but there are a lot of intricacies that can trip up developers. To start understanding closures, we need to start with scope � in Javascript, a function has access to it�s parent�s context, grandparent�s context, etc. up the scope chain. To see this try the code,
var X = "window"; function outermost() { var Y = "outermost"; function inner() { // Knows about the parent function's Y alert(Y); function innermost() { // Knows about the grandparent function's Y alert(Y); // Knows about the greatgrandparent's X alert(X); } innermost(); } inner(); } outermost();
This seems simple enough, even expected. Now here�s the trick, let�s return some function pointers,
var X = "window"; function outermost() { var Y = "outermost"; function inner() { // Knows about the parent function's Y alert(Y); function innermost() { // Knows about the grandparent function's Y alert(Y); // Knows about the greatgrandparent's X alert(X); } // 'inner' returns a reference to the 'innermost' function return innermost; } // 'outermost' returns a reference to the 'inner' function return inner; }
Now let�s call these functions,
// Get the reference to the 'inner' function var inr = outermost(); // Call the 'inner' function inr(); // alerts 'outermost' // Get the reference to the innermost function var inmost = inr(); // alerts 'outermost' // Call the 'innermost' function inmost(); // alerts 'outermost' and 'window'
As you can see, when we call the �innermost� function, using the reference returned by the call to �inner�, it still has access to the X and Y values as they were in scope when the �innermost� function was defined. This is Javascript�s closure � capturing information at the point of definition, so that the information can be used at the point of execution. Also the variables are not copies � they are references.
A very common problem that occurs is in the unintended creation of closure,
for (var i = 0; i < 5; i++) { // This creates a closure! elements[i].onclick = function() { alert(i); }; }
Now clicking on any element will alert �4�, why? Because all of the functions we created have a reference (not a copy) to the outer var i, probably not the effect you want. To fix this,
for (var i = 0; i < 5; i++) { // Don't create a function - no closure elements[i].onclick = Handle(i); } function Handle(i) { return function() { alert(i); }; }
You can read up more about closures here.
- Sent (0)
- New
Generate your business videos by AI with voice or just text
chatGPTaz.com
Talk to ChatGPT by your mother language
AppAIVideo
Your first FREE AI Video App
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 Online
Swap Faces Video, Photo & GIFs Instantly with Powerful AI Tools - Faceswap AI Online FREE
Faceswap AI Online
Swap Faces Video, Photo & GIFs Instantly with Powerful AI Tools - Faceswap AI Online FREE
Faceswap AI Online
Swap Faces Video, Photo & GIFs Instantly with Powerful AI Tools - Faceswap AI Online FREE
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 Free $500 for New Users
Claim Free Temu $500 Credit via Affiliate & Influencer Program
Free TikTok Ads Credit
Master TikTok Ads for Your Business Marketing
Dall-E-OpenAI.com
Generate creative images automatically with AI
chatGPT4.win
Talk to ChatGPT by your mother language
First AI Product from Elon Musk - Grok/UN.com
Speak to Grok AI Chatbot with Your Language
Tooly.win
Open tool hub for free to use by any one for every one with hundreds of tools
GateIO.gomymobi.com
Free Airdrops to Claim, Share Up to $150,000 per Project
iPhoneKer.com
Save up to 630$ when buy new iPhone 16
Buy Tesla Optimus Robot
Order Your Tesla Bot: Optimus Gen 2 Robot Today for less than $20k
Javascript is ... Reply
Reply
Jquery instead of javascript ... @ Cao Phong Reply
For example : Javascript:
document.style.color = "red"; --> so complicated to remember
Jquery:
$(div).css ("color","red"); ->is that okey for coding ????