javascript命名函数表达式 - 范围可访问性 [英] javascript named function expressions - scope accessibility

查看:120
本文介绍了javascript命名函数表达式 - 范围可访问性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遵循 John Resig的JS忍者幻灯片的秘密,我发现了一些东西我不清楚。下面的代码定义了一个命名的函数表达式:

  var ninja = function myNinja(){
console.log(myNinja); // function myNinja(){...}
};
myNinja; //未定义

正如我所看到的,在当前范围内(假设它是全局的) ninja 是保存对指定函数 myNinja 的引用的变量。 ninja 变量在范围中是可访问的 - 这是很清楚的,但 myNinja 在范围中是不可访问的(但它可在内部访问它自己的功能)。



如果我定义一个函数(不使用函数表达式,而是函数声明):

 函数Cheese(){
console.log(Cheese);
}

那么它可以在当前范围内访问。我知道这就是这样 - 但有人可以解释为什么吗?

John Resig对这个概念作了精彩的解释。



http://jsninja.com/



以下是本书中的引用: 4.2.4。内联命名函数

 < script type =text / javascript> 
var ninja =函数myNinja(){
assert(ninja == myNinja,这是一次命名两件事!);
};
ninja();
assert(typeof myNinja ==undefined,
但myNinja没有在函数之外定义。);
< / script>




此列表提出了关于内联
最重要的一点函数:尽管内联函数可以被命名,但是这些名称是
,只能在函数本身中看到。



请记住我们在第3章中讨论的范围规则? 嵌入
函数名称的作用与变量名称相似,它们的作用域是
,仅限于声明它们的函数。



3.2.1。范围和功能


变量声明的范围从声明点
结束它们声明的函数
,无论
是否嵌套。


如果您想要知道更多关于这个概念,这本书将帮助你。


I'm following John Resig's Secrets of JS ninja slides and I've found something I don't clearly understand. Following code defines a named function expression:

var ninja = function myNinja(){
  console.log(myNinja); // function myNinja() {...}
};
myNinja; // undefined

As I can see, in the current scope (suppose it's global), ninja is the variable that holds reference to the named function myNinja. ninja variable is accessible in the scope - that's clear, but myNinja is not accessible in the scope (but it's accessible inside its own function). How come?

If I define a function (not using a function expression, but function declaration):

function Cheese() {
  console.log(Cheese);
}

then it's accessible in the current scope. I know that this just works like that - but can someone explain why is that?

解决方案

In his book, The Secrets of the JavaScript Ninja, John Resig makes a wonderful explanation about this concept.

http://jsninja.com/

Below are the quotes from the book:

4.2.4. Inline named functions

 <script type="text/javascript">
  var ninja = function myNinja(){ 
      assert(ninja == myNinja, "this is named two things at once!"); 
  };
  ninja(); 
  assert(typeof myNinja == "undefined",
    "But myNinja isn't defined outside of the function."); 
 </script>

This listing brings up the most important point regarding inline functions: even though inline functions can be named, those names are only visible within the functions themselves.

Remember the scoping rules we talked about back in chapter 3? Inline function names act somewhat like variable names, and their scope is limited to the function within which they’re declared.

3.2.1. Scoping and functions

Variable declarations are in scope from their point of declaration to the end of the function within which they’re declared, regardless of block nesting.

If you would like to know more about this concept, this book will help you.

这篇关于javascript命名函数表达式 - 范围可访问性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆