分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow

也欢迎大家转载本篇文章。分享知识,造福人民,实现我们中华民族伟大复兴!

Closures are functions whose lexical representation includes variables that aren’t evaluated, meaning that functions are capable of using variables defined outside of the function itself. Using global variables in ECMAScript is a simple example of a closure. Consider the following example:

var sMessage = "Hello World!";function sayHelloWorld() {    alert(sMessage);}sayHelloWorld();

In this code, the variable sMessage isn’t evaluated for the function sayHelloWorld() while the scripts is being loaded into memory. The function captures sMessage for later use, which is to say that the interpreter knows to check the value of sMessage when the function is called. When sayHelloWorld() is called (on the last line), the value of sMessage is assigned and the message “Hello World!” is displayed.

Closures can get more complicated, as when you are defining a function inside of another function, as shown here:

var iBaseNum = 8;function addNumbers(iNum1, iNum2) {    function doAddition() {        return iNum1 + iNum2 + iBaseNum;    }    return doAddition();}

Here, the function addNumbers() contains a function (the closure) named doAddition(). The internal function is a closure because it captures the arguments of the outer function, iNum1 and iNum2, as well as the global variable iBaseNum. The last step of addNumbers() calls the inner function, which adds the two arguments and the global variable and returns the value. The important concept to grasp here is that doAddition() doesn’t accept any arguments at all; the values it uses are captured from the execution environment.

As you can see, closures are a very powerful, versatile part of ECMAScript that can be used to perform complex calculations. Just as when you use any advanced functionality, exercise caution when using closures because they can get extremely complex.

注:被称为Closure的function必须在其外层function的作用域范围内,即必须将其函数定义写在其外层函数的大括号内({})。

给我老师的人工智能教程打call!http://blog.csdn.net/jiangjunshow

JavaScript - Closure相关推荐

  1. javascript closure

    闭包:主要是解决 1.函数闭包的出现是javascript 无权限概念,一个函数里面有全局变量,               这样就麻烦了,大家都能用,存的时间长,所以闭包里的变量只能自己函数用,   ...

  2. Javascript闭包——懂不懂由你,反正我是懂了

    摘要:"如果你不能向一个六岁的孩子解释清楚,那么其实你自己根本就没弄懂."好吧,我试着向一个27岁的朋友就是JS闭包(JavaScript closure)却彻底失败了. 越来越觉 ...

  3. Google JavaScript Style Guide

    转自:http://google.github.io/styleguide/javascriptguide.xml Google JavaScript Style Guide Revision 2.9 ...

  4. javascript 闭包_了解JavaScript闭包:实用方法

    javascript 闭包 Learning a new language involves a series of steps, whereas its mastery is a product o ...

  5. 每个JavaScript开发人员应该知道的33个概念

    每个JavaScript开发人员应该知道的33个概念 介绍 创建此存储库的目的是帮助开发人员在JavaScript中掌握他们的概念.这不是一项要求,而是未来研究的指南.它基于Stephen Curti ...

  6. 最佳JavaScript示例

    JavaScript is the most widely used scripting language on earth. Here are some examples of key syntax ...

  7. Python闭包及其作用域

    Python闭包及其作用域 关于Python作用域的知识在python作用域有相应的笔记,这个笔记是关于Python闭包及其作用域的详细的笔记 如果在一个内部函数里,对一个外部作用域(但不是全局作用域 ...

  8. python中闭包的作用_Python闭包及其作用域

    Python闭包及其作用域 关于Python作用域的知识在python作用域有相应的笔记,这个笔记是关于Python闭包及其作用域的详细的笔记 如果在一个内部函数里,对一个外部作用域(但不是全局作用域 ...

  9. [译]其实闭包并不高深莫测

    其实闭包并不高深莫测 标签: JavaScript closure translate 本文由 伯乐在线 - 刘健超-J.c 翻译,Namco 校稿.未经许可,禁止转载! 英文出处:Igor Šarč ...

最新文章

  1. Hadoop CDH4.5 HBase部署
  2. ASP超级链接和HTML函数正则表达式 修正版
  3. 配置虚拟机和网络配置
  4. Elasticsearch架构原理
  5. java与java ee_计划Java EE 7批处理作业
  6. 浅谈对程序员的认识_浅谈IT界程序员大佬普遍对性的追求
  7. php 日期加减处理函数,php日期加减处理函数示例
  8. 全面介绍Windows内存管理机制及C++内存分配实例(二):内存状态查询
  9. 【API进阶之路】无法想象!大龄码农的硬盘里有这么多宝藏
  10. 深度神经进化大有可为?Uber详解如何用它优化强化学习 | 5篇论文
  11. python脚本-记录Python脚本的运行日志的方法
  12. 计算机存储单位以及内存原理
  13. html5 廖雪峰,廖雪峰 JavaScript Python Git 教程.pdf-原创力文档
  14. Oracle 计算时间差
  15. STM32F407VET6+cubemx+FSMC+ST7789
  16. order by使用索引列排序时会失效吗?
  17. 国内外电商平台反爬虫机制报告
  18. java-在IDEA中,如何在代码和注释之间自动添加空格?
  19. 计算机音乐念诗之王,小可儿《念诗之王》[FLAC/MP3-320K]
  20. antdvue的table合计行

热门文章

  1. 今天有了意外收获,原来还可以这样提交数据的
  2. 【发现】彻底清除www.go2000.cc的清除方法
  3. .net中窗体之间的数据交换总结
  4. NHibernate one-to-one
  5. platform下的js分析_1
  6. Python打包PyPI上传实践
  7. 一些关于Rust在2019年的思考
  8. Python技巧之“is”对比“==”
  9. Oracle等待事件Enqueue CI:Cross Instance Call Invocation
  10. linux系统安装mysql