...

View Full Version : var self = this - An OO question.



RadarBob
03-08-2005, 11:22 PM
I've see this line in some OO javascript, in the constructor (of the parent or child, I forget). Something about a fix for a bug with inheritance? Anyone know exactly?

liorean
03-08-2005, 11:59 PM
Oh, nothing much about that. The this keyword is bound at invokation time to the object a method was invoked from, or from the global object if invoked as a function. Methods are functions, and functions are first class (can be passed around as values) in JavaScript, so assigning them elsewhere would destroy that binding. Variables on the other hand are scoped, and because JavaScript creates closures, all outer variables will remain intact when the function is used elsewhere. Also, inner functions invoked by an outer function will have their own this, while you might want to use the this of the outer function for some actions. Then it's useful to be able to access that from the inner function.

If you want to know why it's used like that in a particular case or so, show me the code and I'll try to tell you.

RadarBob
03-09-2005, 12:31 AM
Well, basically I'm trying to use the inherited function validate(). When i wrote all this code, my spidey sense was telling me something was wrong - my "this" referencing perhaps, and sure enough; I get a "... does not support this method error.

NOTE: don't fret about the "CSForm.getField..." . suffice it to say I get a checkbox.




function MutuallyExclusiveObject () {

// thses functions must be overridden
this.set = function () { } // set()

this.unSet = function () { } //unSet()

this.isSet = function () { } // isSet()


//don't override these functions
this.syncToMe = function (otherObject) {
if (this.isSet()) {
otherObject.unSet();
}
} // syncToA()

this.validate = function () {
var isValid = true;
return isValid;
} // validate()
} // MutualyExclusiveObject Object

function oCheckbox (theCB) {
this.box = theCB;

this.set = function () {
this.box.setValue(1);
} //set()


this.unSet = function () {
this.box.setValue(0);
} // unSet()


this.isSet = function () {
var isSet = true;

if (this.box.getValue() == 1) {
isSet = true;
}else{
isSet = false;
}

return isSet;
} // isSet()

this.toString = function() {
var myThing = "Checkbox Object:\n";

myThing += "\n name:\t" + this.box.getName();
myThing += "\nvalue:\t" + this.box.getValue();

return myThing;
} //toString()
}// oCheckbox
oCheckbox.prototype.MutuallyExclusiveObject = new MutuallyExclusiveObject;

// the following code works
partTransYes = new oCheckbox(CSForm.getField("PR4000_CB_PartTransfYes"));
alert (partTransYes );

// the following code gives me an error
alert(partTransYes.validate());

liorean
03-09-2005, 01:53 AM
I think the problem lies here:
Checkbox.prototype.MutuallyExclusiveObject = new MutuallyExclusiveObject;It's the prototype you want to set, not the prototype's MutuallyExclusiveObject property.

RadarBob
03-09-2005, 02:55 AM
I think the problem lies here:
Checkbox.prototype.MutuallyExclusiveObject = new MutuallyExclusiveObject;It's the prototype you want to set, not the prototype's MutuallyExclusiveObject property.
Jeeez, let's play stump the dummy. I agree this is the source of the problem, but I'm at a loss for the right solution.

First, I see many "variations on a theme" as I research JS inheritance, a source of confusion. This syntax is what I've seen elsewhere.

But basically my understanding is that the syntax above creates an "entry" in/on the oCheckbox.prototype property called "MutuallyExclusiveObject"; which is in turn assigned a "MutuallyExclusiveObject" object. Thus their respective prototypes are bound, or linked if you will.

Now, when a method (or property) is referenced and not found directly in the oCheckbox object, we start looking at the prototype and 'follow the white rabbit' to the MutuallyExclusiveObject's prototype to the inherited function. Thus we have "prototype inheritance."

I suppose I could write it like this:


oCheckbox.prototype.validate = function () { // interesting stuff here
};


But I don't want repeat this code umpteen times; there will be more functions to inherit and more objects which will inherit. And I want to limit the potential for namespace problems.

liorean
03-09-2005, 03:06 AM
Have you tried this?
oCheckbox.prototype=new MutuallyExclusiveObject;

RadarBob
03-09-2005, 02:47 PM
Have you tried this?
oCheckbox.prototype=new MutuallyExclusiveObject;
Is that a hint? ;) Thx Dr. L.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum