Forum Moderators: open

Message Too Old, No Replies

Private class methods

         

anjanesh

5:10 pm on Sep 17, 2007 (gmt 0)

10+ Year Member



Hi

How do I get private methods in an object to work?


function ajax()
{
this.xhr = false; // AJAX XMLHttpRequest Variable

function NewXMLHttpRequest()
{
if (window.XMLHttpRequest) // native XMLHttpRequest object
{
try { this.xhr = new XMLHttpRequest(); }
catch(e) { this.xhr = false; }
}
else if (window.ActiveXObject) // IE/Windows ActiveX version
{
try { this.xhr = new ActiveXObject("Msxml2.XMLHTTP"); }
catch(e)
{
try { this.xhr = new ActiveXObject("Microsoft.XMLHTTP"); }
catch(e) { this.xhr = false; }
}
}
}

NewXMLHttpRequest();
}

var a = new ajax();
alert(a.xhr);


This returns false when it shouldn't be. Using FF 2.0.0.6.

I dont want to change function NewXMLHttpRequest() to this.NewXMLHttpRequest = function () { ... }

Thanks

Fotiman

7:54 pm on Sep 17, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



The problem here is scope. Your private method thinks that 'this' is the window object. Try this:


function ajax()
{
var that = this;
this.xhr = false; // AJAX XMLHttpRequest Variable
var NewXMLHttpRequest = function()
{
if (window.XMLHttpRequest) { // native XMLHttpRequest object
try {
that.xhr = new XMLHttpRequest();
}
catch(e) {
that.xhr = false;
}
}
else if (window.ActiveXObject) { // IE/Windows ActiveX version
try {
that.xhr = new ActiveXObject("Msxml2.XMLHTTP");
}
catch(e) {
try {
that.xhr = new ActiveXObject("Microsoft.XMLHTTP");
}
catch(e) {
that.xhr = false;
}
}
}
};
NewXMLHttpRequest();
}
var a = new ajax();
alert(a.xhr);

anjanesh

8:16 pm on Sep 17, 2007 (gmt 0)

10+ Year Member



Thanks for your reply. That was interesting...this being the window object.

Anyway, I learnt that nested methods are replicated instead of being shared across instances. That would be a waste having a private method for each instance.


function ajax()
{
this.xhr = false; // AJAX XMLHttpRequest Variable
this.NewXMLHttpRequest();
}

ajax.prototype.NewXMLHttpRequest = function()
{
}

Fotiman

8:40 pm on Sep 17, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month




Anyway, I learnt that nested methods are replicated instead of being shared across instances. That would be a waste having a private method for each instance.

A waste? It depends on what you're doing. If you want to ensure that the method is private and can only be called from within the object instance, then it's going to be a requirement. But if you want a public method that can be called from outside of the object instance, then adding it to the prototype would be the better alternative.

Note, this issue of "wasting" memory by replicating methods for each instance is probably not as big of an issue as you might think. Are you going to be passing your object to other functions after you create it? Are you going to be creating many instances of your object? Even if you do, the impact for today's computer is insignificant (in my experience).

Also, newer methods for creating objects are gaining in popularity. Lookup JSON (JavaScript Object Notation).

Hope that helps.