javascript dereference?

Discussion in 'Javascript' started by a.iliadis@gmail.com, Aug 5, 2006.

  1. Guest

    Hi,

    I have the following code:

    function abc(){
    eval('function __newfunc(){alert("hello");}');
    }
    abc();

    now outside of that script context I try to call __newfunc(); and it
    fails.
    In firefox and opera all I had to do is change eval to window.eval to
    force it to
    execute in the window context. Unfortunetly that does not work with IE.
    However
    I could just do the following: window.__newfunc = __newfunc. and force
    it to register.
    My question is how can I do that without knowing the function name of
    the one being
    defined inside the eval expression, in this case __newfunc().

    Thanks
     
    , Aug 5, 2006
    #1
    1. Advertising

  2. writes:

    > function abc(){
    > eval('function __newfunc(){alert("hello");}');


    Which could just as well be:
    function __newfunc(){alert("hello");}

    > }
    > abc();
    >
    > now outside of that script context I try to call __newfunc(); and it
    > fails.


    As it should.

    > In firefox and opera all I had to do is change eval to window.eval
    > to force it to execute in the window context. Unfortunetly that does
    > not work with IE.


    It's not guaranteed to work like that, or at all. It's a proprietary
    feature of those browsers that is not matched by other browsers. That
    happens all the time, usually in the opposite direction :)

    > However I could just do the following: window.__newfunc =
    > __newfunc. and force it to register.


    That's just assigning to a property of the global object, which makes
    the assigned value available as a global variable.

    > My question is how can I do that without knowing the function name
    > of the one being defined inside the eval expression, in this case
    > __newfunc().


    First of all, why do you want to use eval? Is the function declaration
    provided by the user or in some other way not known when the abc
    function was written?

    Assuming the function comes as a string (bad choice, but it happens):

    function abc(funcString) {
    var f = eval("("+funcString+")");
    window[f.name] = f;
    }

    /L
    --
    Lasse Reichstein Nielsen -
    DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
    'Faith without judgement merely degrades the spirit divine.'
     
    Lasse Reichstein Nielsen, Aug 5, 2006
    #2
    1. Advertising

  3. Lasse Reichstein Nielsen wrote:


    > function abc(funcString) {
    > var f = eval("("+funcString+")");
    > window[f.name] = f;

    ^^^^^^
    MS JScript does not expose a name property on function objects. Nor does
    the script engine used in Opera 8 or 9.


    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
     
    Martin Honnen, Aug 5, 2006
    #3
  4. Guest

    Lasse Reichstein Nielsen wrote:
    > writes:
    >
    > > function abc(){
    > > eval('function __newfunc(){alert("hello");}');

    >
    > Which could just as well be:
    > function __newfunc(){alert("hello");}
    >
    > > }
    > > abc();
    > >
    > > now outside of that script context I try to call __newfunc(); and it
    > > fails.

    >
    > As it should.
    >
    > > In firefox and opera all I had to do is change eval to window.eval
    > > to force it to execute in the window context. Unfortunetly that does
    > > not work with IE.

    >
    > It's not guaranteed to work like that, or at all. It's a proprietary
    > feature of those browsers that is not matched by other browsers. That
    > happens all the time, usually in the opposite direction :)
    >
    > > However I could just do the following: window.__newfunc =
    > > __newfunc. and force it to register.

    >
    > That's just assigning to a property of the global object, which makes
    > the assigned value available as a global variable.
    >
    > > My question is how can I do that without knowing the function name
    > > of the one being defined inside the eval expression, in this case
    > > __newfunc().

    >
    > First of all, why do you want to use eval? Is the function declaration
    > provided by the user or in some other way not known when the abc
    > function was written?
    >
    > Assuming the function comes as a string (bad choice, but it happens):
    >
    > function abc(funcString) {
    > var f = eval("("+funcString+")");
    > window[f.name] = f;
    > }
    >


    Unfortunetly none of the two is the case. Infact I dont know what the
    javascript code would be. It may define from 1-n number of functions.
    Also I cant know before hand what the function name would be.
    I am trying to find a way to execute the javascript code without having
    to do something like:
    document.write('<script language="javascript"> code snip here
    </script>');


    > /L
    > --
    > Lasse Reichstein Nielsen -
    > DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
    > 'Faith without judgement merely degrades the spirit divine.'
     
    , Aug 5, 2006
    #4
  5. Martin Honnen <> writes:

    > Lasse Reichstein Nielsen wrote:
    >
    >
    >> function abc(funcString) {
    >> var f = eval("("+funcString+")");
    >> window[f.name] = f;

    > ^^^^^^
    > MS JScript does not expose a name property on function objects. Nor
    > does the script engine used in Opera 8 or 9.


    True. And I thought I checked in Opera, but it must have been Firefox.

    Well, in that case, one will have to pick the name out of the string:

    function abc(funcString) {
    var f = eval("("+funcString+")");
    var nameMatch = /function\s+([\w$]+)\s*\(/.exec(funcString);
    if (nameMatch) {
    window[nameMatch[1]] = f;
    }
    }

    /L
    --
    Lasse Reichstein Nielsen -
    DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
    'Faith without judgement merely degrades the spirit divine.'
     
    Lasse Reichstein Nielsen, Aug 5, 2006
    #5
  6. JRS: In article <44d488c8$0$24888$-online.net>,
    dated Sat, 5 Aug 2006 14:02:14 remote, seen in
    news:comp.lang.javascript, Martin Honnen <> posted :
    >
    >
    >Lasse Reichstein Nielsen wrote:
    >
    >
    >> function abc(funcString) {
    >> var f = eval("("+funcString+")");
    >> window[f.name] = f;

    > ^^^^^^
    >MS JScript does not expose a name property on function objects. Nor does
    >the script engine used in Opera 8 or 9.


    Let the function be F : will the second word in F.toString() always be
    F, the 'name' of the function?
    --
    © John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
    <URL:http://www.jibbering.com/faq/>? JL/RC: FAQ of news:comp.lang.javascript
    <URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
    <URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
     
    Dr John Stockton, Aug 6, 2006
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Howard
    Replies:
    0
    Views:
    386
    Howard
    Jul 1, 2003
  2. Jakob Bieling
    Replies:
    0
    Views:
    437
    Jakob Bieling
    Jul 1, 2003
  3. Joe Seigh
    Replies:
    18
    Views:
    835
    tom_usenet
    Sep 22, 2003
  4. Matthias Kaeppler

    Dereference Adaptor

    Matthias Kaeppler, Feb 27, 2005, in forum: C++
    Replies:
    3
    Views:
    463
    Kurt Krueckeberg
    Feb 28, 2005
  5. Denis Palmeiro

    NULL Pointer Dereference

    Denis Palmeiro, Jul 8, 2003, in forum: C Programming
    Replies:
    10
    Views:
    670
    Shill
    Jul 16, 2003
Loading...

Share This Page