By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,638 Members | 2,241 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,638 IT Pros & Developers. It's quick & easy.

OOP using javascipt

P: n/a
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(toplevel) {
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.specific();
}
}

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

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

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_ClassAField1; //WHY do I need to prefix by this?
...
}
ClassB.prototype.specific = specific;

//Register ClassB class(constructor), 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
Share this Question
Share on Google+
8 Replies


P: n/a
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(toplevel) {
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.specific();
}
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(this); //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

P: n/a
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(toplevel) {
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.specific();
}


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(this); //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

P: n/a
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(toplevel) {
* * *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.specific();
* * *}


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(this); //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.com/groups?as_q=inherit+OR+inheritance+OR+prototype+OR +prototypal&as_ugroup=comp.lang.javascript&scoring =d&filter=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#ps1Post>
PointedEars
Feb 1 '06 #4

P: n/a
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(toplevel) {
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.specific();
}
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(this); //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="javascript">
/************************************************** ******/
/***** ClassManager definition ************/
/************************************************** ******/
function ClassManager(toplevel) {
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.specific();
}
}

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

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

this.method1 = new function() {
var a = this.m_ClassAField1
}
}
/************************************************** ******/
/***** 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_ClassAField1; //WHY do I need to prefix by this?
alert('specific:'+var1);
}
ClassB.prototype.specific = specific;

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

Manager.methodCM();
</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.com/groups?as_q=inherit+OR+inheritance+OR+prototype+OR +prototypal&as_ugroup=comp.lang.javascript&scoring =d&filter=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#ps1Post>
PointedEars

Feb 1 '06 #5

P: n/a
VK

Titatoutati wrote:
<script language="javascript">
/************************************************** ******/
/***** ClassManager definition ************/
/************************************************** ******/
function ClassManager(toplevel) {
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(toplevel) {...
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

P: n/a
VK wrote:
Titatoutati wrote:
<script language="javascript">
/************************************************** ******/
/***** ClassManager definition ************/
/************************************************** ******/
function ClassManager(toplevel) {
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(toplevel) {...
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.specific();
}

and why

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

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_ClassAField1 = '';
}

function method1() {
var a = this.m_ClassAField1;
}
ClassA.prototype.method1 = method1;

But :

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

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

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

P: n/a
An do you know why I need to do that :

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

and why

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

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.specific();
}
}

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

P: n/a
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="javascript">
Should be

<script type="text/javascript">

See also
<URL:http://groups.google.com/groups?as_q=language%3D%22javascript%22&as_ugroup= comp.lang.javascript&scoring=d&filter=0>
[...]
function ClassManager(toplevel) {
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.specific();
}
}

Manager = new ClassManager(this); //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_ClassAField1 = '';

* * *this.method1 = new function() { ^^^ * * * * var a = this.m_ClassAField1
* * *}
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 FunctionExpression 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_ClassAField1; //WHY do I need to prefix by this?
Because the _calling object_ is relevant.
alert('specific:'+var1);
}
ClassB.prototype.specific = specific;

//Register ClassB class(constructor), 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.methodCM();
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_ParentField1 = arg1;
}

Parent.prototype.method1 = function()
{
return this.m_ParentField1;
}

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('specific:' + this.m_ParentField1)
}

// 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 discussion thread is closed

Replies have been disabled for this discussion.