Javascript object Self reference

P

Philip

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
 
L

Lasse Reichstein Nielsen

Philip said:
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(propertyValue)
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
 
D

Douglas Crockford

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.
 
P

Philip

Lasse Reichstein Nielsen said:
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><title>Test Page</title>
<script language=javascript>
<!--//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();return
false;window.event.returnValue=false;">Click</a>
</body></html>
 
L

Lasse Reichstein Nielsen

Philip said:
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
 
D

Douglas Crockford

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><title>Test Page</title>
<script language=javascript>
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();return
false;window.event.returnValue=false;">Click</a>
</body></html>

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

Philip

Lasse Reichstein Nielsen said:
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!
 
T

Thomas 'PointedEars' Lahn

Lasse said:
(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.netscape.com/library/manuals/2000/javascript/1.5/guide/intro.html#1013678>

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.netscape.com/library/manuals/2000/javascript/1.5/guide/preface.html#1003515>
<http://devedge.netscape.com/library/manuals/2000/javascript/1.5/reference/preface.html#1003515>


PointedEars
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top