473,703 Members | 3,125 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

OOP using javascipt

Hi all,

So, what I am trying do to is to manage some classes with javascript.
It's working, but there are some things that I don't understand...
Next is my sample code, and I have indicated the questions in comment
Thanks for the help (I am working with IE 6)

Bruno

/*************** *************** *************** ***********/
/***** ClassManager definition ************/
/*************** *************** *************** ***********/
function ClassManager(to plevel) {
var tip = toplevel //WHY do I need to pass this object to find my
ClassB constructor?

this.methodCM() = function(){
var aClassBObject = new tip['ClassB']('','');
anSvgElement.sp ecific();
}
}

Manager = new ClassManger(thi s); //I need to pass a top level
object.... WHY?

/*************** *************** *************** ***********/
/***** ClassA definition ************/
/*************** *************** *************** ***********/
function ClassA(arg1, arg2)
{
this.m_ClassAFi eld1 = '';

this.method1 = new function() {
var a = m_ClassAField1 //here, I don't need to prefix by this, but in
ClassB I need it...
}
}
/*************** *************** *************** ***********/
/***** ClassB definition, extends ClassA ************/
/*************** *************** *************** ***********/
function ClassB(arg1, arg2) {
// INHERITANCE
this.temp = ClassA;
this.temp(arg1, arg2);
delete this.temp
}

function specific() {
var var1 = this.m_ClassAFi eld1; //WHY do I need to prefix by this?
...
}
ClassB.prototyp e.specific = specific;

//Register ClassB class(construct or), to be able to access it through
the manager. WHY? Is there not an other way to do that?
this['ClassB'] = ClassB;
Feb 1 '06 #1
8 1286
Titatoutati wrote:
So, what I am trying do to is to manage some classes with javascript.
You cannot really. There are no classes in JavaScript beyond the
JavaScript 2.0 test implementation codenamed Epimetheus.
[...]
function ClassManager(to plevel) {
var tip = toplevel //WHY do I need to pass this object to find my
ClassB constructor?
You do not have to.
this.methodCM() = function(){ ^^ var aClassBObject = new tip['ClassB']('','');
anSvgElement.sp ecific();
}
This is a syntax error. A CallExpression must not be on the left-hand
side of a simple assignment (error message: "invalid assignment left-hand
side").

<URL:http://jibbering.com/faq/#FAQ4_43>
}

Manager = new ClassManger(thi s); //I need to pass a top level
object.... WHY?
It is surprising that this works in the first place as it does not make
much sense, and for several reasons I doubt very much that it does work.
One of these is that you are passing a reference to an object which is
operand of the `new' operation later. The object must be one to which
internal [[Construct]] method can be applied in a way. Usually this
applies to certain host objects (like Image) and Function objects only.
[...]


Whatever the rest of your questions, please read previous discussions on
(prototypal) inheritance in JavaScript and ECMAScript Ed. 3 implementations
before you continue.
PointedEars
Feb 1 '06 #2
Thanks for your answer!!

Thomas 'PointedEars' Lahn wrote:
Titatoutati wrote:
So, what I am trying do to is to manage some classes with javascript.
You cannot really. There are no classes in JavaScript beyond the
JavaScript 2.0 test implementation codenamed Epimetheus.

ok, it's not named classes, but for me the result is the same.
[...]
function ClassManager(to plevel) {
var tip = toplevel //WHY do I need to pass this object to find my
ClassB constructor?
You do not have to.
this.methodCM() = function(){

^^
var aClassBObject = new tip['ClassB']('','');
anSvgElement.sp ecific();
}


This is a syntax error. A CallExpression must not be on the left-hand
side of a simple assignment (error message: "invalid assignment left-hand
side").

Ok that was just a mistake, I wrote this code as "javascript pseudo code
like", so in my true code (I don't want to post bussiness related
code...) the () are not present. So this part works.
<URL:http://jibbering.com/faq/#FAQ4_43>
}

Manager = new ClassManger(thi s); //I need to pass a top level
object.... WHY?
It is surprising that this works in the first place as it does not make
much sense, and for several reasons I doubt very much that it does work.
One of these is that you are passing a reference to an object which is
operand of the `new' operation later. The object must be one to which
internal [[Construct]] method can be applied in a way. Usually this
applies to certain host objects (like Image) and Function objects only.

Yes may be it should not work but... it works. And I don't understand
why it should not work, since tip['ClassB'] references ClassB object.
But the problem is not really there. I tried to find a way to not have
to pass this in parameter, but I didn't find. But the code that I have
posted works pretty fine. So, how can can I avoid the "tip" (toplevel)
trick?
[...]


Whatever the rest of your questions, please read previous discussions on
(prototypal) inheritance in JavaScript and ECMAScript Ed. 3 implementations
before you continue.

Is it an other thread? I didnt find it, can you give me please the url?
It's explained why I need to prefix by this in a subclass and not in the
super class? (well ok it's not classes... :) )
PointedEars

Bruno
Feb 1 '06 #3
Titatoutati wrote:
Thomas 'PointedEars' Lahn wrote:
Titatoutati wrote:
So, what I am trying do to is to manage some classes with javascript.
You cannot really. There are no classes in JavaScript beyond the
JavaScript 2.0 test implementation codenamed Epimetheus.

ok, it's not named classes, but for me the result is the same.


You have no clue about the features of class-based inheritance, have you?
function ClassManager(to plevel) {
¬* ¬* ¬*var tip = toplevel ¬* //WHY do I need to pass this object to find
my ClassB constructor?

[...]
¬* ¬* ¬*this.methodCM () = function(){

¬*¬* ¬* ¬* ¬* ¬* ¬* ¬* ¬* ¬* ^^
¬* ¬* ¬*var aClassBObject = new tip['ClassB']('','');
anSvgElement.sp ecific();
¬* ¬* ¬*}


This is a syntax error. A CallExpression must not be on the left-hand
side of a simple assignment (error message: "invalid assignment left-hand
side").

Ok that was just a mistake, I wrote this code as "javascript pseudo code
like", so in my true code (I don't want to post bussiness related
code...) the () are not present. So this part works.


Impossible to say if and why it does (not) work.
Manager = new ClassManger(thi s); //I need to pass a top level
object.... WHY?


It is surprising that this works in the first place as it does not make
much sense, and for several reasons I doubt very much that it does work.
One of these is that you are passing a reference to an object which is
operand of the `new' operation later. The object must be one to which
internal [[Construct]] method can be applied in a way. Usually this
applies to certain host objects (like Image) and Function objects only.

Yes may be it should not work but... it works. And I don't understand
why it should not work, since tip['ClassB'] references ClassB object.
But the problem is not really there. I tried to find a way to not have
to pass this in parameter, but I didn't find. But the code that I have
posted works pretty fine. So, how can can I avoid the "tip" (toplevel)
trick?


Unless you post real code, or at least a working example, this question
probably cannot be answered in a way you would like. So far the correct
answer is to avoid this nonsensical code altogether.
[...]

Whatever the rest of your questions, please read previous discussions on
(prototypal) inheritance in JavaScript and ECMAScript Ed. 3
implementations before you continue.

Is it an other thread?


Other threads, yes. Numerous of them.
I didnt find it, can you give me please the url?


,-<URL:http://groups.google.c om/groups?as_q=inh erit+OR+inherit ance+OR+prototy pe+OR+prototypa l&as_ugroup=com p.lang.javascri pt&scoring=d&fi lter=0>
|
| Results 1 - 10 of 4,870 for inherit OR inheritance OR prototype OR
| prototypal group:comp.lang .javascript

And please learn to quote:

<URL:http://jibbering.com/faq/faq_notes/pots1.html#ps1P ost>
PointedEars
Feb 1 '06 #4
Thomas 'PointedEars' Lahn wrote:
Titatoutati wrote:
Thomas 'PointedEars' Lahn wrote:
Titatoutati wrote:
So, what I am trying do to is to manage some classes with javascript.
You cannot really. There are no classes in JavaScript beyond the
JavaScript 2.0 test implementation codenamed Epimetheus. ok, it's not named classes, but for me the result is the same.


You have no clue about the features of class-based inheritance, have you?

If you say so...
function ClassManager(to plevel) {
var tip = toplevel //WHY do I need to pass this object to find
my ClassB constructor?
[...]
this.methodCM() = function(){
^^
var aClassBObject = new tip['ClassB']('','');
anSvgElement.sp ecific();
}
This is a syntax error. A CallExpression must not be on the left-hand
side of a simple assignment (error message: "invalid assignment left-hand
side").

Ok that was just a mistake, I wrote this code as "javascript pseudo code
like", so in my true code (I don't want to post bussiness related
code...) the () are not present. So this part works.


Impossible to say if and why it does (not) work.

....
Manager = new ClassManger(thi s); //I need to pass a top level
object.... WHY?
It is surprising that this works in the first place as it does not make
much sense, and for several reasons I doubt very much that it does work.
One of these is that you are passing a reference to an object which is
operand of the `new' operation later. The object must be one to which
internal [[Construct]] method can be applied in a way. Usually this
applies to certain host objects (like Image) and Function objects only.

Yes may be it should not work but... it works. And I don't understand
why it should not work, since tip['ClassB'] references ClassB object.
But the problem is not really there. I tried to find a way to not have
to pass this in parameter, but I didn't find. But the code that I have
posted works pretty fine. So, how can can I avoid the "tip" (toplevel)
trick?


Unless you post real code, or at least a working example, this question
probably cannot be answered in a way you would like. So far the correct
answer is to avoid this nonsensical code altogether.

That was real code.
but now is corrected version that works. So now you can say why it's
working and how to avoid my poor nonsensical code (with IE always)?
<script language="javas cript">
/*************** *************** *************** ***********/
/***** ClassManager definition ************/
/*************** *************** *************** ***********/
function ClassManager(to plevel) {
var tip = toplevel //WHY do I need to pass this object to find my
ClassB constructor?

this.methodCM = function(){
var aClassBObject = new tip['ClassB']('','');
aClassBObject.s pecific();
}
}

Manager = new ClassManager(th is); //I need to pass a top level
object.... WHY?

/*************** *************** *************** ***********/
/***** ClassA definition ************/
/*************** *************** *************** ***********/
function ClassA(arg1, arg2)
{
this.m_ClassAFi eld1 = '';

this.method1 = new function() {
var a = this.m_ClassAFi eld1
}
}
/*************** *************** *************** ***********/
/***** ClassB definition, extends ClassA ************/
/*************** *************** *************** ***********/
function ClassB(arg1, arg2) {
// INHERITANCE
this.temp = ClassA;
this.temp(arg1, arg2);
delete this.temp
}

function specific() {
var var1 = this.m_ClassAFi eld1; //WHY do I need to prefix by this?
alert('specific :'+var1);
}
ClassB.prototyp e.specific = specific;

//Register ClassB class(construct or), to be able to access it through
the manager. WHY? Is there not an other way to do that?
this['ClassB'] = ClassB;

Manager.methodC M();
</script>
[...]
Whatever the rest of your questions, please read previous discussions on
(prototypal) inheritance in JavaScript and ECMAScript Ed. 3
implementations before you continue.

Is it an other thread?


Other threads, yes. Numerous of them.
I didnt find it, can you give me please the url?


,-<URL:http://groups.google.c om/groups?as_q=inh erit+OR+inherit ance+OR+prototy pe+OR+prototypa l&as_ugroup=com p.lang.javascri pt&scoring=d&fi lter=0>
|
| Results 1 - 10 of 4,870 for inherit OR inheritance OR prototype OR
| prototypal group:comp.lang .javascript

And please learn to quote:

<URL:http://jibbering.com/faq/faq_notes/pots1.html#ps1P ost>
PointedEars

Feb 1 '06 #5
VK

Titatoutati wrote:
<script language="javas cript">
/*************** *************** *************** ***********/
/***** ClassManager definition ************/
/*************** *************** *************** ***********/
function ClassManager(to plevel) {
var tip = toplevel //WHY do I need to pass this object to find my
ClassB constructor?


You don't need it. You pass "this" from the global context to your
ClassManager, so "toplevel" argument simply contains a reference to the
current global object, which is (in normal circumstances) "window"
object. You can get the same reference at any time by simply calling
self or window property.

Try instead
ClassManager(to plevel) {...
this:
ClassManager() {
var toplevel = self;
...
and you notice no difference. What exactly are you trying to achieve? A
standards A > B > C inheritance or some special case?

Feb 2 '06 #6
VK wrote:
Titatoutati wrote:
<script language="javas cript">
/*************** *************** *************** ***********/
/***** ClassManager definition ************/
/*************** *************** *************** ***********/
function ClassManager(to plevel) {
var tip = toplevel //WHY do I need to pass this object to find my
ClassB constructor?


You don't need it. You pass "this" from the global context to your
ClassManager, so "toplevel" argument simply contains a reference to the
current global object, which is (in normal circumstances) "window"
object. You can get the same reference at any time by simply calling
self or window property.

Try instead
ClassManager(to plevel) {...
this:
ClassManager() {
var toplevel = self;
...
and you notice no difference. What exactly are you trying to achieve? A
standards A > B > C inheritance or some special case?

ok thanks!! Why do you say in normal circumstances?

An do you know why I need to do that :

this.methodCM = function(){
var aClassBObject = new self['ClassB']('','');
aClassBObject.s pecific();
}

and why

this.methodCM = function(){
var aClassBObject = new ClassB('','');
aClassBObject.s pecific();
}

does not work with IE (it works with firefox) ?
And yes I am trying to achieve simple A>B inheritance.
One thing I noticed, tell me if I'm right please :

If I want to have access from ClassB to the methods declared in ClassA,
I can't do :

function ClassA(arg1, arg2)
{
this.m_ClassAFi eld1 = '';
}

function method1() {
var a = this.m_ClassAFi eld1;
}
ClassA.prototyp e.method1 = method1;

But :

function ClassA(arg1, arg2)
{
this.m_ClassAFi eld1 = '';

this.method1 = new function() {
var a = this.m_ClassAFi eld1
}
}

It seems logical, but so, each time I create a ClassB (or Class A)
object I duplicate method1? right?

Is there one other way to do?

And one last thing , when I want to access to m_ClassAField1 from
ClassB, I need to prefix it by this and sometimes (why?) from ClassA too?

thx,
Bruno
Feb 2 '06 #7
An do you know why I need to do that :

this.methodCM = function(){
var aClassBObject = new self['ClassB']('','');
aClassBObject.s pecific();
}

and why

this.methodCM = function(){
var aClassBObject = new ClassB('','');
aClassBObject.s pecific();
}

does not work with IE (it works with firefox) ?


Ok I can do that now (using self) :
function ClassManager() {
this.methodCM = function(){
var aClassBObject = new self.ClassB('', '');
aClassBObject.s pecific();
}
}

But why with firefox I don't need to prefix by self?

Now I don't know when to use self, this or top or othing.... I will need
to read the specification :)

Bruno
Feb 2 '06 #8
Titatoutati wrote:
Titatoutati wrote:
Thomas 'PointedEars' Lahn wrote:
Titatoutati wrote:
> So, what I am trying do to is to manage some classes with javascript.
You cannot really. *There are no classes in JavaScript beyond the
JavaScript 2.0 test implementation codenamed Epimetheus.
ok, it's not named classes, but for me the result is the same.
You have no clue about the features of class-based inheritance, have
you?

If you say so...


Think about information hiding for a start.
That was real code.
It was not.
[...] So now you can say why it's working
and how to avoid my poor nonsensical code
I can. However, you could have done the research that has been
suggested to you explicitly as well.
(with IE always)?
This does not have anything to do with the (targeted) user agent.
<script language="javas cript">
Should be

<script type="text/javascript">

See also
<URL:http://groups.google.c om/groups?as_q=lan guage%3D%22java script%22&as_ug roup=comp.lang. javascript&scor ing=d&filter=0>
[...]
function ClassManager(to plevel) {
As I said, you do not need or want a "class manager". The inheritance
method used in the programming language is prototype-based.
var tip = toplevel //WHY do I need to pass this object to find my ^^^ ClassB constructor?
Because it is used later.
this.methodCM = function(){
var aClassBObject = new tip['ClassB']('',''); ^^^^^^^^^^^^^ aClassBObject.s pecific();
}
}

Manager = new ClassManager(th is); //I need to pass a top level
object.... WHY?
Because the passed reference is used as the base object of a property
access later. Globally declared functions are properties of the Global
Object and `this' in global context refers to the Global Object.
function ClassA(arg1, arg2)
{
* * *this.m_ClassAF ield1 = '';

* * *this.method1 = new function() { ^^^ * * * * var a = this.m_ClassAFi eld1
* * *}
While it is already inefficient (and, as was discussed here, error-prone) to
create a new function object every time an object of the prototype is
created, now this is real nonsense. A FunctionExpress ion already creates a
function object. What is the point in duplicating it? What is this method
to do anyway? `a' is a local variable and is out of scope outside of the
method.
}

function ClassB(arg1, arg2) {
// INHERITANCE
this.temp = ClassA;
this.temp(arg1, arg2);
delete this.temp
}

function specific() {
var var1 = this.m_ClassAFi eld1; //WHY do I need to prefix by this?
Because the _calling object_ is relevant.
alert('specific :'+var1);
}
ClassB.prototyp e.specific = specific;

//Register ClassB class(construct or), to be able to access it through
the manager. WHY? Is there not an other way to do that?
this['ClassB'] = ClassB;
This is nonsense, nothing is "registered ". In essence, it is the same as
x = x. See above.
Manager.methodC M();
A constructor and proper application of prototype-based inheritance can take
care of all of this. Summarized, avoiding the misleading "class" term
references:

function Parent(arg1, arg2)
{
this.m_ParentFi eld1 = arg1;
}

Parent.prototyp e.method1 = function()
{
return this.m_ParentFi eld1;
}

function Child(arg1, arg2)
{
// or simply PrototypeA.call (this);
this.temp = Parent;
this.temp(arg1, arg2);
delete this.temp;
}

Child.prototype = new Parent();

// prototype object fix in case you want to identify Child objects later
Child.prototype .constructor = Child;

Child.prototype .specific = function()
{
window.alert('s pecific:' + this.m_ParentFi eld1)
}

// create a new child object and initialize it
var b = new Child('x', 'y');

// call a method specific to Child objects
b.specific();

// works; method is inherited from Parent through the prototype chain
window.alert(b. method1());

// ReferenceError; not a Child object
(new Parent('x', 'y')).specific( );
</script> [Top post]


Will you learn to quote?
PointedEars
Feb 2 '06 #9

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

Similar topics

1
2910
by: Chris Dean | last post by:
Hi I'm useing the php NUKE CMS and am designing a theme for it. this basically consists of the the main html structure of the page being produced line by line from php echo commands I have a javascript menu system I want to use which is held in an external ..js file so I try to echo out the relevent <script></script> startements to load up this file however it's not showing up on the page - however the static html version of the
1
1496
by: Roberto Gallo | last post by:
Hi again. Thank you for previews answers. I have another question.... How can I inform a Javascipt, that must submit my Applet's data, that this same Applet has produced the data in question? Or in other words, is there any way to call a JavaScript junction within the Applet? Is it the best way? What do you suggest? Thank you very much again,
9
1463
by: Dennis Allen | last post by:
Hi. Anyone here use FrontPage? I've been thinking of using Frontpage to take my web site, www.dennisallen.com, and go frameless. Since I have a left framed frame.htm with lots of javascipt code, is this a good idea? I could use FP insert/components/web page to add my frame.htm page to all other pages. But frame.htm has lots of javascript functions in the header, and needs onload= to start them. I suppose I could move my <script>...
12
1609
by: Karen Baron | last post by:
I'm attempting to login to a website and I noticed that it uses standard javascipt settings. For some reason, the code will not activate and therefore I cannot login to the website. I unblocked the website and enabled javascript. Any other ideas to aid my situation? Thanks! -kb
7
21307
by: Privacy Advocate | last post by:
//crossposted to: comp.lang.javascript, alt.comp.lang.javascript in an effort to get factual answers from JavaScript experts// Simply put; Is it possible to obtain the real (actual) IP address of someone (client) that visits a web site through an anonymous proxy if this person ONLY has JavaScript enabled in their browser? This is NOT a question about PHP, perl, VBScript, Java(.class), or ActiveX. Let us _only_ deal with JavaScript for...
1
1307
by: cool2005 | last post by:
I am new to Opera and tried to use its "javascipt console" and only saw "Java console" and "Error console" under tools|advance. Any idea? thanks mark
5
2722
by: SM | last post by:
Hello, I have an <ul>, and when i click on a item i want to add a class to that item. The class itself will change some display properties, using CSS. See code below. But, whenever i click on a item, the CSS properties, always applies to the last item in the list. How can i achieve this using JavaScript and the DOM?
1
6703
by: pingalkar | last post by:
hi, I want to read time from http response header , using javascript in Hr , Minute and Second form. Pls help with code snipet. thanks & regards, Gajendra
1
1487
by: joe | last post by:
I often would like see the HTML output produces by my Javascipt code for debugging. Is this possible?
0
8750
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...
0
9246
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9112
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9005
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
7856
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
6585
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
5922
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4680
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2058
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.