javascript statically scope
在javascript 里面, 函数中使用的未定义的变量,会默认变为全局的变量。 而通过 var 这个关键字定义的变量,就是局部变量。
As far as the output is concerned myVar and addMe both will be global variable in this case , as in javascript if you don't declare a variable with var then it implicitly declares it as global hence when you call runMe() then myVar will have the value 10 and addMe will have 20 .
---------------------------------------------------------------------------------------------------------------------------
Are variables statically or dynamically “scoped” in javascript?
Or more specific to what I need:
If I call a function from within another function, is it going to pull the variable from within the calling function, or from the level above? Ex:
myVar=0;
function runMe(){
myVar = 10;
callMe();
}
function callMe(){
addMe = myVar+10;
}
What does myVar end up being if callMe() is called through runMe()?
-----------------------------------------------------------------------------------------------------
Jeff is right. Note that this is not actually a good test of static scoping (which JS does have). A better one would be:
myVar=0;
function runMe(){
var myVar = 10;
callMe();
}
function callMe(){
addMe = myVar+10;
}
runMe();
alert(addMe);
alert(myVar);
In a statically scoped language (like JS), that alerts 10, and 0. The var myVar (local variable) in runMe shadows the global myVar in that function. However, it has no effect in callMe, so callMe uses the global myVar which is still at 0.
In a dynamically scoped language (unlike JS), callMe would inherit scope from runMe, so addMe would become 20. Note that myVar would still be 0 at the alert, because the alert does not inherit scope from either function.
javascript statically scope的更多相关文章
- javascript作用域(Scope),简述上下文(context)和作用域的定义
网页制作Webjx文章简介:这篇文章将正面解决这个问题:简述上下文(context)和作用域的定义,分析可以让我们掌控上下文的两种方法,最后深入一种高效的方案,它能有效解决我所碰到的90%的问题. 作 ...
- JavaScript作用域[[scope]]
[[scope]] : 隐式的属性 每个JavaScript函数都是一个对象,对象中有些属性可以访问,而有些属性是不可以访问的,这些属性仅供JavaScript引擎存取, [[scope]]就是其中一 ...
- [Javascript] Function scope
We have code like: var numbers = [1,2,3]; for(var i in numbers){ setTimeout(function(){console.log(n ...
- JavaScript闭包的底层运行机制
转自:http://blog.leapoahead.com/2015/09/15/js-closure/ 我研究JavaScript闭包(closure)已经有一段时间了.我之前只是学会了如何使用它们 ...
- JAVASCRIPT的一些知识点梳理
春节闲点,可以安心的梳理一下以前不是很清楚的东东.. 看的是以下几个URL: http://web.jobbole.com/82520/ http://blog.csdn.net/luoweifu/a ...
- JavaScript的语法要点 3 - Calling Context
上一篇讲了JavaScript的Scope Chain - 每一个函数都有一个scope chain与之关联,scope chain上有第一个对象维护着本地变量作为其属性.另外我们在JavaScrip ...
- Scope Directive
---------------------------Scope-------------------------------- https://docs.angularjs.org/guide/sc ...
- How do JavaScript closures work?
Like the old Albert Einstein said: If you can't explain it to a six-year-old, you really don't under ...
- JavaScript 作用域链解析
JavaScript 中有 Scope( 作用域 ) , Scope chain( 作用域链 ) , Execute context( 执行上下文 ) , Active Object ( 活动对象 ) ...
随机推荐
- http升级https改造方案
一.解决方案 1.httpClient请求https版蓝鲸接口 (1).原理 https与http最大的区别在于SSL加密传输协议的使用.在自己写的JAVA HttpClient程序,想手动验证证书, ...
- mysql 常用的语句
1.查出当前的字段,放到一个字段中:GROUP_CONCAT(distinct b.patent_ip) ,如 (select GROUP_CONCAT(distinct b.patent_ip) f ...
- day25-1 网络架构与互联网组成
目录 网络架构 单机架构 CS架构 数据放在服务端和客户端的利与弊 BS架构 互联网和互联网的组成 互联网的硬件组成 互联网的软件组成 网络架构 单机架构 应用领域: 单机游戏 CS架构 基于网络,应 ...
- vue vueRouter vuex Axios webpack 前端常用内容
Axios 是一个基于 promise 的 HTTP 库,可以用在浏览器和 node.js 中.
- vue工程化之公有CSS、JS文件
1.关于公共的css 在src下面新建public.css,然后在main.js中引入进来 import '@/public.css',这样所有页面中都会使用这个样式了,如果只是部分页面需要,那还是不 ...
- PyTorch学习笔记——softmax和log_softmax的区别、CrossEntropyLoss() 与 NLLLoss() 的区别、log似然代价函数
1.softmax 函数 Softmax(x) 也是一个 non-linearity, 但它的特殊之处在于它通常是网络中一次操作. 这是因为它接受了一个实数向量并返回一个概率分布.其定义如下. 定义 ...
- 诊断:ORA-16188: LOG_ARCHIVE_CONFIG settings inconsistent with previously started instance
11g数据库里面一个套RAC环境,之前搭建过DG,后来拆除掉.某次演练重启的时候,碰到数据库无法open的情况.日志显示 WARNING: The 'LOG_ARCHIVE_CONFIG' init. ...
- css布局的各种FC简单介绍:BFC,IFC,GFC,FFC
什么是FC? Formatting Context,格式化上下文,指页面中一个渲染区域,拥有一套渲染规则,它决定了其子元素如何定位,以及与其他元素的相互关系和作用. BFC 什么是BFC Block ...
- 正则表达式 整理(\w \s \d 点 贪婪匹配 非贪婪匹配 * + ? {} | [] ^ $ \b 单词边界 分组、re.findall()、re.split()、re.search()、re.match()、re.compile()、re.sub())
re.findall 匹配到正则表达式的字符,匹配到的每个字符存入一个列表,返回一个匹配到的所有字符列表 一. 匹配单个字符 import re # \w 匹配所有字母.数字.下划线 re.find ...
- LeetCode(48)Rotate Image
题目 You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise ...