This JavaScript article tutorial guides you a full completed instructions and JavaScript code examples to use this JavaScript object. You can learn many basic JavaScript knowledges such as scope, call a function/method, etc.
Please go to the full post page for all details or read other related JavaScript tutorials:
- JavaScript Objects: Compare and Clone
- Let Get Started Object-Oriented Programming JavaScript
- Object JavaScript Test Quiz
- Simple Concepts about Types and Objects in JavaScript OOP
- Object Oriented JavaScript Techniques
- Demo
- Enlarge
- Reload
- New window
Free iPage Web Hosting for First Year NOW
If you're still looking for a reliable web host provider with affordable rates, why you don't take a little of time to try iPage, only with $1.89/month, included $500+ Free Extra Credits for the payment of 24 months ($45)?
Over 1,000,000+ existisng customers can not be wrong, definitely you're not, too! More important, when you register the web hosting at iPage through our link, we're going to be happy for resending a full refund to you. That's awesome! You should try iPage web hosting for FREE now! And contact us for anything you need to know about iPage.
2. Module madness
This one will give you a headache. Examine the following code which uses a module pattern:
window.WhoAmI = "I'm the window object";
var Module = function() {
this.WhoAmI = "I'm the Module object";
function Test() {
this.WhoAmI = "I'm still the Module object";
}
return {
WhoAmI: WhoAmI,
Test: Test
};
}();
alert(Module.WhoAmI); // I'm the Module object
alert(window.WhoAmI); // I'm the Module object
Module.Test();
alert(Module.WhoAmI); // I'm still the Module object
The code looks logical � so why is window.WhoAmI
saying it�s the module object?
We need to remember that we have a self-executing function. It�s results are returned to the Module
variable but, when it�s first run, Module doesn�t exist. this
is therefore the global window object. In other words, this.WhoAmI
= window.WhoAmI
= "I'm the Module object"
.
The function returns a JavaScript object with a WhoAmI
property with a value of 'WhoAmI'
. But what does that refer to? In this case, the JavaScript interpreter works up its prototype chain until it magically finds window.WhoAmI
("I'm the Module object"
).
Finally, we run the Test()
method. However, Module
has now been created so, within the Test
function, this
refers to the Module
object so it can correctly set the WhoAmI
property.
In summary, avoid using this
within a module to refer to the module itself! You should never need it.
3. Method misconceptions
Here�s another JavaScript pattern which will screw with your synapses:
var myObject = {};
myObject.method = function() {
this.WhoAmI = "I'm myObject.method";
function Test() {
this.WhoAmI = "I'm myObject.method.Test()";
}
Test();
return this.WhoAmI;
};
alert(myObject.method()); // I'm myObject.method
In this example, Test()
is a private function executed within myObject.method()
. At first glance, you would expect this
within Test()
to reference myObject
. It doesn�t: it refers to the global window object since it�s just another function.
If you want to reference myObject
within the private function, you�ll require a closure, for example:
var myObject = {};
myObject.method = function() {
this.WhoAmI = "I'm myObject.method";
var T = this;
function Test() {
T.WhoAmI = "I'm myObject.method.Test()";
}
Test();
return this.WhoAmI;
};
alert(myObject.method()); // I'm myObject.method.Test()
4. Referencing methods
Here�s a little code which, fortunately, will work exactly as you expect:
var myObject = {};
myObject.WhoAmI = "I'm myObject";
myObject.method = function() {
this.WhoAmI = "I'm myObject.method";
};
// examine properties
alert(myObject.WhoAmI); // I'm myObject
myObject.method();
alert(myObject.WhoAmI); // I'm myObject.method
Let�s make a minor change and assign myObject.method
to another variable:
// examine properties
alert(myObject.WhoAmI); // I'm myObject
var test = myObject.method;
test();
alert(myObject.WhoAmI); // I'm myObject
Why hasn�t myObject.WhoAmI
changed? In this case, the call to test()
acts like a regular function call so this
refers to the window object rather than myObject
.
If you think that�s nasty, wait until we take a look at JavaScript event handlers in my next post!
- 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