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 10 of 10
  1. #1
    New to the CF scene
    Join Date
    Sep 2008
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts

    return not in function

    This is seriously giving me a huge headache. When uFuncs is 'alert("half")' it does what you expect it to alerts a then half then b. but when uFuncs='return' it alerts a then throws error "return not in function". How can i make the return be evaluated?

    Code:
    function test(uFuncs)
    {
    	
    alert('a');
    eval(uFuncs);
    alert('b');
    	
    }
    Thanks!

  • #2
    Master Coder
    Join Date
    Feb 2003
    Location
    Umeň, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    eval cannot introduce abrupt termination into a scope. So, no return.
    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
    Senior Coder
    Join Date
    Mar 2005
    Location
    Portsmouth UK
    Posts
    4,521
    Thanks
    3
    Thanked 507 Times in 494 Posts
    What is uFuncs?

    if it is a function name
    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    
    <head>
      <title></title>
    <script language="JavaScript" type="text/javascript">
    /*<![CDATA[*/
    function test(uFuncs)
    {
    
    alert('a');
    window[uFuncs]();
    
    }
    
    function Test(){
    alert('b');
    
    }
    
    /*]]>*/
    </script></head>
    
    <body>
    <input type="button" name="" value="TEST" onclick="test('Test')" />
    </body>
    
    </html>
    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/

  • #4
    Master Coder
    Join Date
    Feb 2003
    Location
    Umeň, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    Even with Vic's solution, you can't add abrupt termination, though. Not without writing an if-statement that returns depending on the return value of whatever you do in the middle.
    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
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    After all, return is not an expression, thus it can not be evaluated.
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #6
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,399
    Thanks
    11
    Thanked 595 Times in 575 Posts
    eval breaks out of the current scope, running at global level. thus, the "return" would not actually be executing in your function.
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/9/03) IE7:0.1, IE8:4.6, IE11:9.1, IE9:3.1, IE10:3.0, FF:17.2, CH:46, SF:11.4, NON-MOUSE:38%

  • #7
    New to the CF scene
    Join Date
    Sep 2008
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Ah i see. So the only way is to add something like this:
    function test(uFuncs)
    {

    alert('a');
    eval(uFuncs);
    if(pseudoReutrn) { return }
    alert('b');

    }

  • #8
    Master Coder
    Join Date
    Feb 2003
    Location
    Umeň, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    Quote Originally Posted by rnd me View Post
    eval breaks out of the current scope, running at global level. thus, the "return" would not actually be executing in your function.
    Actually, that is not true. The code eval runs is always run in the current scope, so if it's run in a function it will be executing in the function. It's actually one of the mistakes in designing JavaScript that Brendan Eich did that we sadly can't get rid of now.
    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

  • #9
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,399
    Thanks
    11
    Thanked 595 Times in 575 Posts
    Quote Originally Posted by liorean View Post
    Actually, that is not true. The code eval runs is always run in the current scope, so if it's run in a function it will be executing in the function. It's actually one of the mistakes in designing JavaScript that Brendan Eich did that we sadly can't get rid of now.
    you are in practice correct. i was thinking of Function code, which is always global. i made and use an eval2 based upon Function, and tired at night, i got them mixed up. please forgive me.

    technically, a new context (an hence scope) is created for eval, and old properties are copied, which is why it is so slow.
    from the spec: 10.2.2 : "The scope chain is initialised to contain the same objects, in the same order, as the calling context's
    scope chain. This includes objects added to the calling context's scope chain by with statements and
    catch clauses.
    "

    at least th OP got the point...

    ps: you can specify the scope for eval in firefox by passing it as a second argument. this will be removed in 3.1 because it can reverse engineer private variables closed inside public objects.
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/9/03) IE7:0.1, IE8:4.6, IE11:9.1, IE9:3.1, IE10:3.0, FF:17.2, CH:46, SF:11.4, NON-MOUSE:38%

  • #10
    Master Coder
    Join Date
    Feb 2003
    Location
    Umeň, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    Quote Originally Posted by rnd me View Post
    technically, a new context (an hence scope) is created for eval, and old properties are copied, which is why it is so slow.
    No a new context is not added, you're reading the spec text wrong. And no, that is not the reason it's slow. The reason it's slow is that it has to fire up a fully featured ECMAScript parser to generate the AST which it has to either interpret or compile into bytecode which in turn can either be interpreted or JIT compiled into machine code to be executed. Or in the case of the V8 engine Google Chrome ships with, skip the bytecode and go directly on JITting the AST into machine code.
    from the spec: 10.2.2 : "The scope chain is initialised to contain the same objects, in the same order, as the calling context's scope chain. This includes objects added to the calling context's scope chain by with statements and catch clauses."
    And if you read this again, you should see the obvious: It's operating not on a new context, but the exact same context, since all objects in the scope of eval are not copies of, but the same objects as the calling context contained.
    at least th OP got the point...

    ps: you can specify the scope for eval in firefox by passing it as a second argument. this will be removed in 3.1 because it can reverse engineer private variables closed inside public objects.
    Yeah, it was a bad idea to allow it, but at least we haven't seen any exploits of it so far, only potential for it. There are similar problems with __proto__ being writable which are also being addressed by the ECMAScript committee (by specifying only read and not write access to the prototype).
    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


  •  

    Posting Permissions

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