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 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Aug 2006
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Javascript Inheritance

    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.

  • #2
    Senior Coder
    Join Date
    Mar 2005
    Location
    Portsmouth UK
    Posts
    4,466
    Thanks
    3
    Thanked 495 Times in 482 Posts
    eval('FunctionName'+'()');

    =

    window['FunctionName']();

    but better
    Vic

    God Loves You and will never love you less.

    http://www.vicsjavascripts.org/Home.htm

    If my post has been useful please donate to http://www.operationsmile.org.uk/

  • #3
    Senior Coder
    Join Date
    Jul 2005
    Location
    New York, NY
    Posts
    1,084
    Thanks
    4
    Thanked 19 Times in 19 Posts
    I'm not sure what you're problem here is, if you have an object prototype that defines methods and members, and then you have another object that uses that prototype and adds to it you still have access to the "parent"'s methods and members unless you redfine them. The prototype chain keeps going back as long you want it to.

    Perhaps if you could provide some code to explain what you're trying to do, we could help you avoid eval altogether.

  • #4
    Registered User
    Join Date
    Oct 2004
    Posts
    592
    Thanks
    0
    Thanked 1 Time in 1 Post
    There are now JavaScript Prototype Class Framework out there.

    You can easily use them to create true OO Javascript.

  • #5
    Senior Coder A1ien51's Avatar
    Join Date
    Jun 2002
    Location
    Between DC and Baltimore In a Cave
    Posts
    2,717
    Thanks
    1
    Thanked 94 Times in 88 Posts
    I think you may want to look at Appenix B of my book. Sun has it avaialble on their site here:

    http://java.sun.com/javascript/ajaxi...ction_ApB.html

    Hope that helps.

    Eric
    Tech Author [Ajax In Action, JavaScript: Visual Blueprint]


  •  

    Posting Permissions

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