[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Javascript OOP encapsulation trouble

Posted on 2007-10-16
8
Medium Priority
?
196 Views
Last Modified: 2007-10-16
Hi,

I am trying to prove some concepts in OO javascript, with the help of the following code;

[code]

function catClass(){
    this.sound = "miau";
    this.makeSound = function(){
        alert(this.sound);
    }
}

var cat = new catClass();

[/code]

This code is held inside the <head> part of the HTML document.

If I call the function cat.makeSound(); then the correct alert will show ("miau").

But if I use this command:

window.onload = cat.makeSound;

Then it shows [undefined] as alert message! That means it loses its reference, I know, and I would like to know how to prevent that.

Thanks in advance!
0
Comment
Question by:stdev07
  • 6
  • 2
8 Comments
 
LVL 23

Expert Comment

by:basicinstinct
ID: 20084290
it works if you do this:

<body onload="cat.makeSound();">
0
 
LVL 23

Expert Comment

by:basicinstinct
ID: 20084296
or this:

window.onload = function(){cat.makeSound();};
0
 
LVL 23

Expert Comment

by:basicinstinct
ID: 20084298
even (oddly) this:

window.onload = cat.makeSound();

??
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 23

Expert Comment

by:basicinstinct
ID: 20084313
oh no, i was getting confused, it just got called at that point, sorry...
0
 
LVL 23

Expert Comment

by:basicinstinct
ID: 20084324
THis definitely works:

window.onload = new Function("cat.makeSound();");
0
 
LVL 23

Expert Comment

by:basicinstinct
ID: 20084453
so in summary, these definitely work:

window.onload = new Function("cat.makeSound();");

OR

<body onload="cat.makeSound();">
0
 
LVL 3

Accepted Solution

by:
KhoiNqq earned 2000 total points
ID: 20084735
The cause of loose reference here is when window.onload is fired, it will call the cat.makeSound function BUT with the executive context is 'window' not 'cat', it's means the 'this' keyword in the makeSound function  is reference to window, not cat object. To change the executive context, you should do:

window.onload = function (){ return cat.makeSound.apply(cat); }

Best regards,
KhoiNqq
0
 
LVL 3

Expert Comment

by:KhoiNqq
ID: 20084743
Put in the onload attribute will works also:
 <BODY onload="cat.makeSound.apply(cat);">
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

CTAs encourage people to do something specific to show interest in your company, product or service. Keep reading to learn why CTAs should always be thought of as extremely important, albeit small, sections of websites.
The first step to building an amazing About page is to figure out what you want the page to say about your company. You then must grab the attention of the reader, boast a bit, tell a story and let others brag about you. With a little bit of thought…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
Suggested Courses
Course of the Month19 days, 1 hour left to enroll

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question