PDA

View Full Version : Private methods in extended classes



After2050
20 Jun 2010, 1:26 AM
Hi Dear Friends,

How can I make a method / field private in extended class?

Dnana | Trigent

defcon1
20 Jun 2010, 3:40 AM
Therefore you can use a constructor method:



// Define constructor method (class) with a private dummy-property
var myClass = function(cfg) {

var privateProperty = "Hello, World!";
this.publicProperty = "Hello, World!";

// Call superclass constructor
myClass.superclass.constructor.call (this, cfg);
}


// Extend the Ext.Panel and put some public methods to it
Ext.extend (myClass, Ext.Panel, {

// public, no access to "privateProperty"
output: function() {

alert (this.publicProperty);
}
});


// Instantiate extended Panel
var myPanel = new myClass ({

renderTo: Ext.getBody(),
title: 'My Panel',
width: 300,
height: 500
});

myPanel.output();
;)
d1

After2050
20 Jun 2010, 10:24 PM
Hi Thanks. How can I make the methods private? I see you mentioned only about properties.

Dnana | Trigent

Condor
20 Jun 2010, 10:35 PM
Example:

MyClass = Ext.extend(OtherClass, (function(){
var privateMethod = function(){
alert('Hello world');
};
return {
publicMethod: function(){
return privateMethod();
}
}
})());

Animal
20 Jun 2010, 11:17 PM
Or, if the private method needs to refer to "this" to use properties of the instance...



MyClass = Ext.extend(OtherClass, (function(){
var privateMethod = function(){
alert('Hello world');
};
return {
publicMethod: function(){
return privateMethod.call(this);
}
}
})());

After2050
23 Jun 2010, 10:02 PM
Hi Guys,

Thanks for all your replies.

Dnana | Trigent

leizz
19 Nov 2010, 8:42 AM
Hi Animal,

Just wonder, is this the correct way of passing in a constructor while having private methods etc?


MyClass = Ext.extend(Ext.util.Observable, (function(){

var private_method = function(){ ... };
var private_property = xxx;

return {
constructor: function(config){
.....
},
public_method1: function { ....},
public_method2: function { ....}
}
})());

darthwes
19 Nov 2010, 9:42 AM
I think protected is the correct keyword, no?

Condor
19 Nov 2010, 9:54 AM
You can create a private method this way, but that is not a private property; it's a private class property (only one value for all class instances).

leizz
19 Nov 2010, 10:32 AM
Thanks Condor.

So 1) the way the constructor is passed in is fine; 2) there seems no way to define private property in JavaScript?

leizz

Animal
19 Nov 2010, 10:44 AM
You are not passing a constructor. It's still a member property of an object which is passed as the second parameter to Ext.extend.

No, you cannot have private, instance properties. You can have private vars which are accessible from functions declared within that anonymous function. But of course there are shared among all instances.

leizz
19 Nov 2010, 11:33 AM
You are not passing a constructor. It's still a member property of an object which is passed as the second parameter to Ext.extend.

Understood. Is it correct to say then the 'constructor' property of the anonymous object will be used to construct my subclass?

thanks,
leizz