Re: OOP using javascipt



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
.



Relevant Pages

  • Re: Terms for method types?
    ... this is just basic prototype ... on this stage - about the exact inheritance mechanics, ... var myInstance = new MyObject; ... First off, I was talking about javascript, not java. ...
    (comp.lang.javascript)
  • Re: Terms for method types?
    ... on this stage - about the exact inheritance mechanics, ... could freely use the regular English words like class, prototype, ... First off, I was talking about javascript, not java. ... In this case your understanding of the nature of the closure is not ...
    (comp.lang.javascript)
  • Re: Odd behavior involving function.apply()
    ... introduced into the created object using this pattern. ... As for the intent, considering JavaScript really doesn't have "types", ... class-based inheritance, and 3) prototypes are -not- classes. ... from a prototype should be capable of acting as a prototype itself. ...
    (comp.lang.javascript)
  • [QUIZ][SUMMARY] Prototype-Based Inheritance (#214)
    ... Prototype based inheritance is used in many programming languages, ... cost method from the parent. ... the child object, the child object maintains a reference to the ...
    (comp.lang.ruby)
  • Verifying if ntfs files/folders rights are inherited or not...
    ... folders where inheritance have been removed or altered with explicit ntfs ... I don't know how to manipulate the ace flags to know if the ... For child objects that are containers, ... ' Retrieve the content of Win32_SecurityDescriptor DACL property. ...
    (microsoft.public.scripting.vbscript)