lexical-scope

Why variable object was changed to lexical environment in ES5?

╄→尐↘猪︶ㄣ 提交于 2019-12-06 03:33:24
问题 ES5 changed variable object(VO) to lexical environment. What's the motivation of such change since VO is already very obvious as perception? 回答1: I think variable objects are more analogous to environment records. An Environment Record records the identifier bindings that are created within the scope of its associated Lexical Environment. In ES5 there are two different kinds of environment records: Declarative environment records are used to define the effect of ECMAScript language syntactic

Why does jQuery has a “window=this” at the very begining and say it would speed up references to window?

徘徊边缘 提交于 2019-12-05 20:05:19
When I open jQuery's source code I find this line. var // Will speed up references to window, and allows munging its name. window = this Why and how this line will speed up? javascript functions have lexical scope jQuery wraps its entire implementation in an anonymous function when said function begins execution it is executing in the "global" scope (ie this == window ). "window = this;" simply creates a local identifier in that scope so that references to it do not have to "bubble up" outside of the local scope to resolve. this would be faster for javascript to reference to, as compared to

Lexical vs dynamic scoping in terms of SICP's Environment Model of Evaluation

喜夏-厌秋 提交于 2019-12-05 15:23:00
问题 In Section 3.2.2 of SICP the execution of the following piece of code (define (square x) (* x x)) (define (sum-of-squares x y) (+ (square x) (square y))) (define (f a) (sum-of-squares (+ a 1) (* a 2))) (f 5) is explained in terms of this diagram. Each time a function is applied, a new frame is created (labeled by E1 through E4 ) which represents a set of bindings between symbols and values. When a symbol is not bound in a frame, that frame's enclosing environment is queried for a binding of

Does my $_; do anything if $_ is implied

↘锁芯ラ 提交于 2019-12-05 05:59:25
I think the answer is yes but I just want to make sure. so if I have sub something { my $_; my @array = ...; while ( @array ) { say; } } is the my $_; actually effective at lexicalizing the parameter passed to the say? In this particular case I'm using the DZP::UnusedVarsTests and it's complaining that I haven't used my $_; and I suspect it's a bug since I'm using it in a case where it's implied. Evan Carroll The short answer is Yes. It makes the functions in that scope use the lexically scoped $_ , and not the global $_ . If they write back to $_ , as in the case of s/// , you will have some

How does JS declare variables and functions without lexical scope in a statement block?

霸气de小男生 提交于 2019-12-05 00:22:45
问题 var a; { function a() {} a = 60; console.log('1: ', a); } console.log('2: ', a); var b; { b = 60; function b() {} console.log('3: ', b); } console.log('4: ', b); The output is: 1: 60 2: f a() {} 3: 60 4: 60 I can't figure out why, if I remove the curly braces, it all prints 60 . Perhaps because of hoisting. But a function declaration doesn't have lexical scope as far as I know, and even if it does, the first output should print the function, right? 来源: https://stackoverflow.com/questions

Something like let in Ruby

眉间皱痕 提交于 2019-12-04 04:44:20
问题 I used to write let-like expressions -- with lexical scope. So I write my own (sad, but it will fail with multiple threads): # Useful thing for replacing a value of # variable only for one block of code. # Maybe such thing already exist, I just not found it. def with(dict, &block) old_values = {} # replace by new dict.each_pair do |key, value| key = "@#{key}" old_values[key] = instance_variable_get key instance_variable_set key, value end block.call # replace by old old_values.each_pair do

JavaScript example question: lexical scoping/closure - Eloquent Javascript

﹥>﹥吖頭↗ 提交于 2019-12-03 17:07:01
问题 So I'm new to programming and I'm trying to learn JS with the book Eloquent Javascript. So far so good, until I reached an example with the following code function makeAddFunction(amount) { function add(number) { return number + amount; } return add; } var addTwo = makeAddFunction(2); var addFive = makeAddFunction(5); show(addTwo(1) + addFive(1)); note: show is like alert, only it shows the variables on the screen of a JS console the tutorial has integrated. The author says this is an example

What are the new rules for variable scoping in Emacs 24?

左心房为你撑大大i 提交于 2019-12-03 16:30:14
问题 Emacs 24 now has lexically-scoped variables. It also still has dynamically-scoped variables, of course. Now that it has both, I'm quite confused about when a variable will have which kind of scope. There's a lexical-binding variable that controls when lexical binding is enabled, and I think I read something about defvar now declaring a dynamically-scoped variable, but in general I'm pretty lost. Is there a good explanation somewhere of Emacs 24's new scoping rules? Or put another way, when I

What are the new rules for variable scoping in Emacs 24?

馋奶兔 提交于 2019-12-03 05:52:15
Emacs 24 now has lexically-scoped variables. It also still has dynamically-scoped variables, of course. Now that it has both, I'm quite confused about when a variable will have which kind of scope. There's a lexical-binding variable that controls when lexical binding is enabled, and I think I read something about defvar now declaring a dynamically-scoped variable, but in general I'm pretty lost. Is there a good explanation somewhere of Emacs 24's new scoping rules? Or put another way, when I look at a variable in Emacs Lisp code written for Emacs 24, how do I tell what scope that variable is

Why are variables declared with “our” visible across files?

半城伤御伤魂 提交于 2019-12-03 02:50:39
From the "our" perldoc : our has the same scoping rules as my, but does not necessarily create a variable. This means that variables declared with our should not be visible across files, because file is the largest lexical scope. But this is not true. Why? You can consider our to create a lexically-scoped alias to a package global variable. Package globals are accessible from everywhere; that's what makes them global. But the name created by our is only visible within the lexical scope of the our declaration. package A; use strict; { our $var; # $var is now a legal name for $A::var $var = 42;