473,768 Members | 8,578 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Javascript object Self reference

Hi all,

Been having a really tricky problem. I'm trying to do some object oriented
programming with javascript and it was all working fine until I had to
support Netscape 4. The problem is that my object doesn't seem to be able
to refer back to itself. Up until now, I've been using code like this...

function Object
{
var Self = this;
this.Property = Property;
this.Method = Method;

function Method ()
{
alert (Self.Property) ;
}
}

This is because when the Method was called by an event handler, you couldn't
use 'this' to refer to the object because it was referring to (I believe)
either the event itself or the window or something. Using Self always
worked on Netscape 6, Mozilla, Opera, IE, etc.. but with Netscape 4 it comes
back saying that Self has no properties, leading me to believe that perhaps
it went out of scope? Is this the case? If so, how could I remedy that? I
need to have some kind of variable or property as part of the object that
can refer back to the object itself.

Thanks!

Philip

--
// Philip D. (ga***@aspNospa minherEtools.bi z)
// ASPTools.Biz (http://www.asptools.biz)
// ASP, ASP.NET and Javascript web applications
Jul 20 '05 #1
7 3513
"Philip" <pa********@spe llnextright.var izun.net> writes:
Up until now, I've been using code like this...

function Object
{
var Self = this;
this.Property = Property;
this.Method = Method;

function Method ()
{
alert (Self.Property) ;
}
}
Well, it could at most have been *like* this, because this doesn't work
(no argument to the function - I guess it should be Property - and the
function is called Object, which is bound to conflict with something).

What was the exact code you used, and how did you use it?
This is because when the Method was called by an event handler, you couldn't
use 'this' to refer to the object because it was referring to (I believe)
either the event itself or the window or something.
When the method is called, you don't use this. This is used when you
create the object with
new Object(property Value)
Inside the body of a handler function, "this" refers to the object
that the handler is on. When you call a method from inside the handler,
the value of "this" is different for that method's body - it is the
object the method is a method of. If you just call a function, not as
a member of an object, "this" refers to the global object.
Using Self always worked on Netscape 6, Mozilla, Opera, IE,
etc.. but with Netscape 4 it comes back saying that Self has no
properties, leading me to believe that perhaps it went out of scope?
No, Javascript has static scope. You can't leave the scope you start
in.
Is this the case?


Probably not, but you need to show us the actual code that has this
problem for us to test it. Just a minimal example that show the
problem. Also, which version of Netscape 4 do you use?

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #2
> Been having a really tricky problem. I'm trying to do some object oriented
programming with javascript and it was all working fine until I had to
support Netscape 4. The problem is that my object doesn't seem to be able
to refer back to itself. Up until now, I've been using code like this...

function Object
{
var Self = this;
this.Property = Property;
this.Method = Method;

function Method ()
{
alert (Self.Property) ;
}
}

This is because when the Method was called by an event handler, you couldn't
use 'this' to refer to the object because it was referring to (I believe)
either the event itself or the window or something. Using Self always
worked on Netscape 6, Mozilla, Opera, IE, etc.. but with Netscape 4 it comes
back saying that Self has no properties, leading me to believe that perhaps
it went out of scope? Is this the case? If so, how could I remedy that? I
need to have some kind of variable or property as part of the object that
can refer back to the object itself.


Can we see more of the actual program? This abstract looks ok.
Jul 20 '05 #3
"Lasse Reichstein Nielsen" <lr*@hotpop.com > wrote in message
news:65******** **@hotpop.com.. .
What was the exact code you used, and how did you use it?


Yes, thanks to both of you for the replies. Sorry about the delay--I had to
boil it down and isolate the problem. I believe the following code
illustrates it perfectly. What to notice in the code... When running this
in Netscape 4.79, an error message comes up in the Javascript console saying
that "Self has no properties".

** Code follows **

<html><head><ti tle>Test Page</title>
<script language=javasc ript>
<!--//Netscape Communicator 4.79 Test
function Ob( Arg )
{
var Self = this;
this.Prop = Arg;
this.Method = Method;
alert (Self.Prop); // Shows that as of construction, Self works

function Method()
{
// Following line works as 'this' but not as 'Self'.
alert(Self.Prop );
}
}

var Global = new Ob("If this shows, it worked!");
//-->
</script></head>
<body>
<a href="#" onclick="Global .Method();retur n
false;window.ev ent.returnValue =false;">Click</a>
</body></html>
--
// Philip Dearmore (pd*******@aspt ools.biz)
// ASPTools.Biz (http://www.asptools.biz)
// ASP, ASP.NET and Javascript web applications
Jul 20 '05 #4
"Philip" <pa********@spe llnextright.var izun.net> writes:
Yes, thanks to both of you for the replies. Sorry about the delay--I had to
boil it down and isolate the problem.
Always a good thing to do!
I believe the following code illustrates it perfectly. What to
notice in the code... When running this in Netscape 4.79, an error
message comes up in the Javascript console saying that "Self has no
properties".


Yes, I get the same in Netscape 4.8. It is clearly a bug.

I reduced the example to just three lines inside Ob:
---
function Ob() {
var Self = this;
this.Method = Methodx;
function Methodx() {alert(Self);}
}
---
and this fails (alerts "undefined" ). Then I tried rearranging the
lines. You can move the "var Self=this" anywhere without changing anyting.
However, the following ones do work (alerts "[object Object])":

---
function Ob() {
var Selfx = this; // again doesn't matter where this line is
function Methodx() {alert(Selfx);}
this.Method = Methodx;
}
---
and
---
function Ob() {
Self = this;
this.Method = Methodx;
// Self = this; // or here, but not between "function" and "var"
function Methodx() {alert(Self);}
var Self;
}
---
So, something goes wrong in how NS4 builds its closures (which is
fairly impressive, it's not really that hard!)

I'm afraid I'm too tired to figure out what on earth they were thinking!
(Isn't JavaScript 1.3 supposed to be ECMAScript v3 compliant? Or was it only
ECMAScript v2?

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #5
> What to notice in the code... When running this
in Netscape 4.79, an error message comes up in the Javascript console saying
that "Self has no properties". <html><head><ti tle>Test Page</title>
<script language=javasc ript> function Ob( Arg )
{
var Self = this;
this.Prop = Arg;
this.Method = Method;
alert (Self.Prop); // Shows that as of construction, Self works

function Method()
{
// Following line works as 'this' but not as 'Self'.
alert(Self.Prop );
}
}

var Global = new Ob("If this shows, it worked!"); </script></head>
<body>
<a href="#" onclick="Global .Method();retur n
false;window.ev ent.returnValue =false;">Click</a>
</body></html>


Your pattern looks clean. What happens when Global.Method is call normally (not
in an event handler)?

Jul 20 '05 #6
"Lasse Reichstein Nielsen" <lr*@hotpop.com > wrote in message
news:ll******** **@hotpop.com.. .
and this fails (alerts "undefined" ). Then I tried rearranging the
lines. You can move the "var Self=this" anywhere without changing anyting.
However, the following ones do work (alerts "[object Object])":
So, something goes wrong in how NS4 builds its closures (which is
fairly impressive, it's not really that hard!)


Wow, you are a genius! Myself being the classic cookie-cutter programmer I
would never have thought to rearrange the declarations. I've rearranged the
code in my original objects and it is working now. I thought I was going to
have to rewrite everything using global variables or something...

Thanks!

--
// Philip Dearmore (pd*******@aspt ools.biz)
// ASPTools.Biz (http://www.asptools.biz)
// ASP, ASP.NET and Javascript web applications
Jul 20 '05 #7
Lasse Reichstein Nielsen wrote:
(Isn't JavaScript 1.3 supposed to be ECMAScript v3 compliant? Or was it only
ECMAScript v2?


JavaScript 1.3 *claims* to be "fully compatible" to the *first* edition
of ECMAScript, see

<http://devedge.netscap e.com/library/manuals/2000/javascript/1.5/guide/intro.html#1013 678>

But Netscape seems to have weird definitions of "based on" and
"fully compatible" anyway. Take for example the unary "+" and
"typeof" operators not supported in some NN3/4 versions despite

<http://devedge.netscap e.com/library/manuals/2000/javascript/1.5/guide/preface.html#10 03515>
<http://devedge.netscap e.com/library/manuals/2000/javascript/1.5/reference/preface.html#10 03515>
PointedEars
Jul 20 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
6753
by: William Krick | last post by:
I did a little searching for javascript code that will make a page "jump out" of frames when the page is loaded. There seems to be many, many ways to do this and I've added the ones I found to the end of this post. Which one should I use? Is there a generally accepted "best" way to do this that works will all browsers? .... Krick
0
9576
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
1
9961
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9843
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8840
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7384
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5283
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5425
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3932
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2808
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.