www.webdeveloper.com
Page 2 of 2 FirstFirst 12
Results 16 to 22 of 22

Thread: Another dumb question about scope

  1. #16
    Join Date
    Dec 2003
    Location
    Bucharest, ROMANIA
    Posts
    15,428
    Let's put all these together in a brief:

    Literal strings, numbers and booleans are primitives. That means they are not objects stricto sensu. But they derived from their correspondent native wrappers: String, Number and Boolean, which are objects, with native properties and methods. Therefor, they inherit those properties/methods through their wrapper's prototype. To be more precise it is a delegation, not an inheritance.

    But if the wrapper is invoked as a constructor (ex: var mystring= new String()), the variable becomes the reference of a plain object, thus it can gain later individual members, same as any other Object.

    Sounds more clear, now?

  2. #17
    Join Date
    Dec 2003
    Location
    Bucharest, ROMANIA
    Posts
    15,428
    Despite primitives, functions are objects. But their content is not a member of that object. Their content is a piece of code, a sub-routine which has to be run whenever the function is called.

  3. #18
    Join Date
    Mar 2011
    Posts
    19
    Quote Originally Posted by Kor View Post
    Despite primitives, functions are objects. But their content is not a member of that object. Their content is a piece of code, a sub-routine which has to be run whenever the function is called.
    Ahhhh, ahhhh, yes wonderful

  4. #19
    Join Date
    Mar 2011
    Posts
    19
    So, given the above, is there a way for a function to refer to itself outside of a constructor without necessarily knowing its own name?

  5. #20
    Join Date
    Dec 2003
    Location
    Bucharest, ROMANIA
    Posts
    15,428
    Quote Originally Posted by goldfidget View Post
    So, given the above, is there a way for a function to refer to itself outside of a constructor without necessarily knowing its own name?
    Yes, I have already mentioned that:
    Code:
    function fnc(){
    var thisFunction=arguments.callee;
    alert(thisFunction)
    }
    fnc();
    There is a small confusion about the fact that arguments.callee is or not deprecated. Well, Mozilla stipulates clearly, I think:

    "JavaScript 1.4: deprecated arguments, arguments.callee, and arguments.length as properties of Function instances; retained arguments as a local variable of a function and arguments.callee and arguments.length as properties of this variable.
    Last edited by Kor; 04-04-2011 at 09:52 AM.

  6. #21
    Join Date
    Mar 2011
    Posts
    19
    Thank you both, that was very helpful

  7. #22
    Join Date
    Jul 2008
    Location
    urbana, il
    Posts
    2,787
    Quote Originally Posted by Kor View Post
    There is a small confusion about the fact that arguments.callee is or not deprecated. Well, Mozilla stipulates clearly, I think:

    "JavaScript 1.4: deprecated arguments, arguments.callee, and arguments.length as properties of Function instances; retained arguments as a local variable of a function and arguments.callee and arguments.length as properties of this variable.
    the "variable" arguments.callee and the fn.arguments "property" are both removed in ECMA5 strict.
    http://ejohn.org/blog/ecmascript-5-s...json-and-more/


    It's a good idea to name all your functions anyways, so this isn't that big of a deal.

    but i will miss argument.callee. nobody seems to talk about it, but as of now, you can even get the caller property:
    Code:
    function  one(){
     return two();
    }
    
    function two(){
     return "two says: " + arguments.callee.caller.toString();
    }
    
    
    alert( one() )
    if it's undefined, guess where the function was called from?



    ecma5 cleans up a lot of this mess, along with inconsistencies resulting from code like "var arguments" or "return arguments".
    i encourage everyone to read the spec, and not just the new parts.
    you will find a lot of familiar routines have the same name, but different and more specific steps.
    Last edited by rnd me; 04-05-2011 at 07:43 PM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

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



Recent Articles