Re: OOP using javascipt
- From: Thomas 'PointedEars' Lahn <PointedEars@xxxxxx>
- Date: Thu, 02 Feb 2006 21:02:45 +0100
Titatoutati wrote:
Titatoutati wrote:If you say so...
Thomas 'PointedEars' Lahn wrote:
Titatoutati wrote:ok, it's not named classes, but for me the result is the same.
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.
You have no clue about the features of class-based inheritance, have
you?
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
.
- References:
- OOP using javascipt
- From: Titatoutati
- Re: OOP using javascipt
- From: Thomas 'PointedEars' Lahn
- Re: OOP using javascipt
- From: Titatoutati
- Re: OOP using javascipt
- From: Thomas 'PointedEars' Lahn
- Re: OOP using javascipt
- From: Titatoutati
- OOP using javascipt
- Prev by Date: Re: Client Side Data Compression (JavaScript)
- Next by Date: Re: Automating date/time input for modem/router
- Previous by thread: Re: OOP using javascipt
- Next by thread: onclick action
- Index(es):
Relevant Pages
|