473,387 Members | 1,379 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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***@aspNospaminherEtools.biz)
// ASPTools.Biz (http://www.asptools.biz)
// ASP, ASP.NET and Javascript web applications
Jul 20 '05 #1
7 3489
"Philip" <pa********@spellnextright.varizun.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(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
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.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><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>
--
// Philip Dearmore (pd*******@asptools.biz)
// ASPTools.Biz (http://www.asptools.biz)
// ASP, ASP.NET and Javascript web applications
Jul 20 '05 #4
"Philip" <pa********@spellnextright.varizun.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/rasterTriangleDOM.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><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)?

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*******@asptools.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.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
Jul 20 '05 #8

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

Similar topics

5
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.