tags:

views:

70

answers:

3

Hy i need to inherent a class from another.

Parent has private var "_data" and private method "_test" and public method "add" Now the child have a public method "add", that uses the private method from the parent "_test" and the private var "_data".

How do i do this?

var Parent = function(){
    var _data = {};
    var _test = function(){alert('test')};
    this.add = function(){alert('add from parent')}
}

var Child = function(){
    this.add = function(){// here uses the _data and _test from the Parent class
        alert('add from child')
    }
}

// something to inherent Child from Parent
// instance Child and use the add method

And i think im miss the prototype concept (http://stackoverflow.com/questions/892467/javascript-inheritance)

A: 

There are some libraries which extend JavaScript with OOP-like features. Maybe you find something in John Resigs Blog Entry on JS inheritance (via base2 and Prototype).

Marcel J.
I can only use jquery as is the framework i use. I prefer to stick to 1 only...
Totty
+1  A: 
Weston C
A: 

A simple inheritance:

function createChild(){
    Child.prototype=new Parent()
    return new Child()
}

This way you get a fresh initialization of Parent for every Child.

eBusiness