SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Member
    Join Date
    Aug 2006
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Javascript Inheritance and Scope

    Hi,

    I am dealing with a very unique problem, one that I have eventually come to assume requires the use of eval. I know that eval is highly inefficient, and so I hope to keep it down to about 3 or 4 evals called at the beginning of a page load, and thats it. Essentially I will need to call eval on every inheritance.
    The problem stems from the fact that in order to truly acheive 'classical-inheritance', I need to the ability to call parent functions, and the parent's parent functions. Right now I have a slightly dirty method of calling the same function down an entire chain, through all ancestors. But if I call a parent's function, and from within that parent function, it calls another function, I want that function to be called from the scope of the Parent, not the Child. And right now, it calls the function from the scope of the Child.
    My idea is to use tokens, like $this, and $parent within the the function definitions, and then replace those tokens with actual references to the Current class and Parent class, ie. Parent.prototype[method].apply(this, arguments); replaces $parent.method(arguments). The only way I can seem to fix this issue is by doing an eval, as I found no acutal way of applying a variable number of arguments to the Function object.
    If anyone else has a better idea I am more than happy to listen. Thank you.

    Perhaps an example would also be of some help
    Code:
    function test() {
      alert('$name');
    }
    
    updateFunction(test, {name: 'value'});
    New source for test
    Code:
    function test() {
       alert('value');
    }
    Again, this is because I need to, at inheritance time, call the functions by the Class name, and not by 'this'.

  2. #2
    SitePoint Enthusiast
    Join Date
    Aug 2006
    Posts
    81
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sorry, I'm really not sure what you're trying to do.. Maybe more of an example would help.

    You can give the Function constructor several arguments, like this:
    Code:
    foo = new Function('x', 'y', 'z', 'return x*y*z;');
    Don't know if that's what you're getting at though.
    I'm available for hire! - www.deftdevelopment.com

  3. #3
    SitePoint Member
    Join Date
    Aug 2006
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Deeper example

    Well, a more extensive example is this.

    Code:
    function A() {}
    //A inherits from Object
    extends(Object, A);
    
    A.prototype.foo = function() {
      alert('A: foo');
    }
    
    A.prototype.bar = function() {
      alert('A: bar');
    }
    
    function B() {}
    
    //B inherits from A
    extends(A, B);
    
    B.prototype.foo = function() {
      //Where parent is the parent class, in this case A
      this.parent.foo();
      alert('B: foo');
    
      //Where this is B
      this.bar();
    
    }
    
    B.prototype.bar = function() {
      //Where parent is the parent class, in this case A
      this.parent.bar();
      alert('B: bar);
    }
    
    
    function C() {}
    //C inherits from B
    extends(B, C);
    
    C.prototype.foo  = function() {
      this.parent.foo();
      alert('C: foo');
    }
    
    C.prototype.bar = function() {
      this.parent.bar();
      alert('C: bar');
    }
    Now what this shows, is multiple levels of inheritance. What I want is to be able to call C.foo(), and have the following happen.

    1. in C.foo() this.parent.foo should call B.prototype.foo
    2. in B.foo() this.parent.foo should call A.prototype.foo
    3. in B.foo() again, this.bar() should call B.prototype.bar

    As it stands now, since in B.foo, it uses the word this, it applies to the object calling, not the scope of the function. So if C calls foo, when B calls this.bar, it calls the C version, not B's, which is improper inheritance.


Bookmarks

Posting Permissions

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