javascript inherance? with Private vars and methods too
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 (Javascript inheritance)
Generally, the way you do inheritance in JavaScript is by assigning the Child
class's prototype property to an instance of the Parent
object. So you'd define your constructor function for Child
:
function Child() { /* initialization yadda yadda */ }
and then do this:
Child.prototype = new Parent();
which is more or less equivalent to Class Child extends Parent
in other languages -- Child then has the methods, members, and, via closure, the private members of a Parent object.
There's a couple of tricky points here. One of them is initialization done in the constructor. Calling the Parent constructor in the Child constructor function explicitly ( function Child() { Parent(); }
) seems to be the most obvious tack, but problematic because of the way this
is handled and returned from an initialization (inside the function Parent
what's this
?). You can work around this by using the apply
method of Function
(which every function has access to):
function Child() {
Parent.apply(this,arguments);
//initialization unique to Child goes here
}
(See http://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Objects/Function/apply for more on apply)
But the way I usually do it is to break all the initialization for classes into a separate "init" function that sortof replaces the constructor.
function Parent() { this.init(); }
Parent.prototype.init = function () { /* initialization yadda yadda */ }
That way, this is simply inherited along with everything else... or I can write a new one that calls the old one if I like:
Child.prototype.init = function () {
Parent.prototype.init.apply(this,arguments);
/* insert stuff unique to child constructor here */
}
The other tricky bit is access to Parent
's private members. You can't do it, really, except by Parent
's methods. They're truly private, not "protected," "subclasses" can't see them except by methods they inherit. There are some ways of writing around that, but that's probably another Q/A, the simplest one is to make sure you design your Parent
well.
A simple inheritance:
function createChild(){
Child.prototype=new Parent()
return new Child()
}
This way you get a fresh initialization of Parent for every Child.
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).
精彩评论