scope

Scope troubles in Javascript when passing an anonymous function to a named function with a local variable

夙愿已清 提交于 2019-12-20 01:35:21
问题 Sorry about the title - I couldn't figure out a way to phrase it. Here's the scenario: I have a function that builds a element: buildSelect(id,cbFunc,...) Inside buildSelect it does this: select.attachEvent('onchange',cbFunc); I also have an array that goes: var xs = ['x1','x2','x3'...]; Given all of these, I have some code that does this: for(var i = 0; i < xs.length; i++) { buildSelect(blah,function(){ CallBack(xs[i],...) },...); } The issue is that when onchange gets fired on one of those

JavaScript Puzzle: Scope

十年热恋 提交于 2019-12-20 01:18:27
问题 So I am trying to figure out this puzzle: function fun1(){ var result = []; for (var i = 0; i < 5; i++){ result.push( function() {return i} ); } return result; } console.log(fun1()[0]()) // returns 5? Shouldn't the first element of that array return a function that returns '0'? 回答1: Let's break down what happens step by step: We declare a function fun1() that returns an array result . The for loop iterates 5 times, with each iteration incrementing i . Notice that the anonymous function

Passing local variable with name of a global variable isn't possible in JS?

牧云@^-^@ 提交于 2019-12-20 01:16:31
问题 foo = "foobar"; var bar = function(){ var foo = foo || ""; return foo; } bar();` This code gives a result empty string. Why cannot JS reassign a local variable with same name as a global variable? In other programming languages the expected result is of course "foobar", why does JS behave like that? 回答1: That's because you declared a local variable with the same name - and it masks the global variable. So when you write foo you refer to the local variable. That's true even if you write it

const within block scopes in Node and Chrome (V8)

耗尽温柔 提交于 2019-12-20 01:07:13
问题 I am writing a nodejs (v4.2.4) app were I encountered some odd behaviour. function A(number) { this.number = number; } for(var i = 0; i < 3; i++) { const a = new A(i); console.log(a.number); } const b = new A(99); console.log(b.number); My intuition, coming from Java (and the one of FireFox), is that the output should have been 0 1 2 99 However, Node (and Chrome) give me 0 0 0 99 I investigated and understood from MSN - block scope that var does not have block scope in javascript. Looking

How can I *unpack* an object into a function's scope?

血红的双手。 提交于 2019-12-19 20:39:01
问题 I have this code... function a(options) { for (var item in options) { if ( ! options.hasOwnProperty(item)) { continue; } this[item] = options[item]; } } a({ 'abc': 'def' }); jsFiddle. Whilst this unpacks variables from the object, it sets them to global scope (attached to window ) because this is window in that circumstance. So after the function I can do alert(abc) and it will alert def , which isn't good. How would I set the scope of the variables to the function? 回答1: If you want to put

How can I *unpack* an object into a function's scope?

心不动则不痛 提交于 2019-12-19 20:38:13
问题 I have this code... function a(options) { for (var item in options) { if ( ! options.hasOwnProperty(item)) { continue; } this[item] = options[item]; } } a({ 'abc': 'def' }); jsFiddle. Whilst this unpacks variables from the object, it sets them to global scope (attached to window ) because this is window in that circumstance. So after the function I can do alert(abc) and it will alert def , which isn't good. How would I set the scope of the variables to the function? 回答1: If you want to put

Scope of class variable with list comprehension [duplicate]

老子叫甜甜 提交于 2019-12-19 20:24:56
问题 This question already has an answer here : Why is one class variable not defined in list comprehension but another is? (1 answer) Closed last year . have a look at the following piece of code: class a: s = 'python' b = ['p', 'y'] c = [x for x in s] the output: >>> a.c ['p', 'y', 't', 'h', 'o', 'n'] but when i try to limit the list with if: class a: s = 'python' b = ['p', 'y'] c = [x for x in s if x in b] Shows the following exception: Traceback (most recent call last): File "<pyshell#22>",

What does scope of functions mean?

妖精的绣舞 提交于 2019-12-19 19:47:21
问题 What does scope of functions mean? I understand the scope of variables. When we talk about the scope of functions, is it referred to the functions within structures (classes) or is there scope for the normal functions that we call in main() of a C/C++ program? 回答1: Functions can have global, namespace, class (usually called members in that case), or local (within another function) scope. They can also be static giving them internal linkage or within an anonymous namespace, making them

Typescript Global Variable across Files

丶灬走出姿态 提交于 2019-12-19 18:24:44
问题 I created a variable in a .ts file that has no module or class. It mostly looks just like a plain JavaScript file. I want this variable to accessible in another .ts file inside of a class that is inside of a variable. So for example I have: foo.ts var foo = "some stuff"; bar.ts module Bar { export class BarClass { function getFoo() { return foo; } } } I'm not sure this is the best way to do it. I've tried using the window.bar global but that doesn't seem to work. I'm new to TypeScript jumping

In what scope is a struct member identifier put?

£可爱£侵袭症+ 提交于 2019-12-19 17:49:31
问题 The C spec says There are four kinds of scopes: function, file, block, and function prototype. Now if I do the following outside any function struct A { int x; }; My understanding is that the identifier x is visible at file-scope. And that we use a namespace syntax to access the member, as the spec says each structure or union has a separate name space for its members (disambiguated by the type of the expression used to access the member via the . or -> operator) Let's make this more clearer