JavaScript Advanced Knowledge Analysis--function access

Source: Internet
Author: User

The code information comes from http://ejohn.org/apps/learn/.

It can be referenced inside it by its name.
function Yell (n) {   return n > 0? Yell (n-1) + "a": "Hiy";} console.log (Yell (4) = = = "Hiyaaaa", "In the inside of the function accesses itself through its name");
How do you call yourself inside a named function when it is not declared?
var ninja = {   function(n) {     return n > 0? arguments.callee (n-1 + "A": "Hiy";   }}; Console.log (Ninja.yell (4) = = "Hiyaaaa", "Arguments.callee refers to the function itself");

The name scope of the function is invalidated.

var function Myninja () {   = = = Myninja, "the reference and name of this function is thesame function"typeof Myninja = = = "Undefined", " But not accessible outside the function " ); Console.log (ninja); // function Myninja ()

Note that when we use a named function declaration to assign to a variable, the name of the function can be referenced internally, but not externally. And the assigned variable can be referenced.

You can also call an anonymous function when it is a property of an object.
var ninja = {   function(n) {     return n > 0? ninja.yell (n-1) + "a": "Hiy";   }; Console.log (Ninja.yell (4) = = = "Hiyaaaa"); // true

Through the properties of this object, we can call it.

What happens when the original object is removed?
 var  ninja = {yell:  function   (n) { return  n > 0?   Ninja.yell (n-1) + "a": "Hiy" ; } }; Console.log (Ninja.yell ( 4) = = "Hiyaaaa"); // true  var  Samurai = {Yell:ninja.yell};  var  ninja = null    try   {Samurai.yell ( 4);  catch   (E) {Console.log ( "cannot be accessed. " 
The same thing, but what happens when we give the anonymous function a name?
var ninja = {   function  yell (n) {     return n > 0? Yell (n-1) + "a": " Hiy ";   }}; Console.log (Ninja.yell (4) = = "Hiyaaaa")  ; var samurai =var ninja = null; Console.log (Samurai.yell (4) = = "Hiyaaaa", " Note that it can still call this function ");

JavaScript Advanced Knowledge Analysis--function access

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.