Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    Regular Coder
    Join Date
    Jun 2002
    Location
    Round Rock, Texas
    Posts
    443
    Thanks
    0
    Thanked 0 Times in 0 Posts

    var self = this - An OO question.

    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?

  • #2
    Master Coder
    Join Date
    Feb 2003
    Location
    UmeŚ, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    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.
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #3
    Regular Coder
    Join Date
    Jun 2002
    Location
    Round Rock, Texas
    Posts
    443
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

    PHP Code:

    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()); 

  • #4
    Master Coder
    Join Date
    Feb 2003
    Location
    UmeŚ, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    I think the problem lies here:
    Code:
    Checkbox.prototype.MutuallyExclusiveObject = new MutuallyExclusiveObject;
    It's the prototype you want to set, not the prototype's MutuallyExclusiveObject property.
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #5
    Regular Coder
    Join Date
    Jun 2002
    Location
    Round Rock, Texas
    Posts
    443
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by liorean
    I think the problem lies here:
    Code:
    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:
    PHP Code:
    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.

  • #6
    Master Coder
    Join Date
    Feb 2003
    Location
    UmeŚ, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    Have you tried this?
    Code:
    oCheckbox.prototype=new MutuallyExclusiveObject;
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #7
    Regular Coder
    Join Date
    Jun 2002
    Location
    Round Rock, Texas
    Posts
    443
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by liorean
    Have you tried this?
    Code:
    oCheckbox.prototype=new MutuallyExclusiveObject;
    Is that a hint? Thx Dr. L.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •