scope

AngularJS overwrites isolated directive scope

冷暖自知 提交于 2019-12-21 12:38:54
问题 Usage: <my-directive my-var="true"></my-directive> Directive: app.directive('myDirective', [ function () { var definition = { restrict: "E", replace: false, transclude: false, scope: { myVar: '@', }, controller: ['$scope', function($scope) { console.log($scope.myVar); // "true" $scope.myVar = "false"; console.log($scope.myVar); // "false" setTimeout(function() { console.log($scope.myVar); // "true" (!) }, 100); }] }; return definition; } ]); Console output "true" "false" "true" What is

Difference between IIFE and call

时光总嘲笑我的痴心妄想 提交于 2019-12-21 09:30:08
问题 Is there a difference between: (function(){ }).call(this); and (function(){ })(); or var MODULE = {}; (function(){ this.hello = 'world' }).call(MODULE); and var MODULE = {}; (function(m){ m.hello = 'world' })(MODULE); I often see the first case in compiled javascript. They both would create a scope and do their namespacing job well. Is there any difference or is it just a matter of taste. Edit: And why would compiled javascript would use call over IIFE? 回答1: (function(){ }).call(this); calls

Why does Grails recommend singleton scope for controllers with actions as methods?

只谈情不闲聊 提交于 2019-12-21 09:28:57
问题 I know early versions of Grails used prototype scope for controllers because actions were all closures at that time. I know that the current version documentation recommends singleton scoped controllers for controllers that use methods as actions. From the following post it seems that methods and singleton scope are more desirable or recommended, but it's not clear why. ttp://grails.1312388.n4.nabble.com/Default-scope-for-controllers-doc-td4657986.html We have a large project that uses

How to get 'this' value of caller function?

只愿长相守 提交于 2019-12-21 09:24:27
问题 If I have a function like this: function foo(_this) { console.log(_this); } function bar() {} bar.prototype.func = function() { foo(this); } var test = new bar(); test.func(); then the test instance of bar gets logged. However, for this to work I have to pass the this in the bar.prototype.func function. I was wondering whether it is possible to obtain the same this value without passing this . I tried using arguments.callee.caller , but this returns the prototype function itself and not the

Python Variable Scope (passing by reference or copy?)

╄→гoц情女王★ 提交于 2019-12-21 07:41:37
问题 Why does the variable L gets manipulated in the sorting(L) function call? In other languages, a copy of L would be passed through to sorting() as a copy so that any changes to x would not change the original variable? def sorting(x): A = x #Passed by reference? A.sort() def testScope(): L = [5,4,3,2,1] sorting(L) #Passed by reference? return L >>> print testScope() >>> [1, 2, 3, 4, 5] 回答1: Long story short: Python uses pass-by-value, but the things that are passed by value are references. The

C# Lambdas and “this” variable scope

大城市里の小女人 提交于 2019-12-21 07:14:14
问题 I am wondering whether I can use the this keyword inside a C# lambda, although actually I know that I can but I want to make sure that this isn't a bad thing or will produce subtle issues later on. Having read the rules on variable scope for lambdas, I can see that: A variable that is captured will not be garbage-collected until the delegate that references it goes out of scope. So this leads me to assume that an object instance ( this ) will also be captured. To test this I wrote this

Can `match` in Racket have patterns with variables from an outer scope?

瘦欲@ 提交于 2019-12-21 07:14:04
问题 Consider the following example: #lang racket (match '(cat . doge) [`(,a . ,b) (match b [a #t] [_ #f])] [_ "Not a pair"]) This is what I might write if I wanted to match pairs where the head and tail are the same. This doesn't work though because the second a is bound as a new variable (and matches anything). Are there any pattern forms which allow me to use the previously bound a from the outer scope? I know this can be achieved in the following way (match* ('cat 'doge) [(a a) #t] [(_ _) #f])

Local/static variable scope in C++

非 Y 不嫁゛ 提交于 2019-12-21 06:18:15
问题 If I write something like this: #include <iostream> int main() { using namespace std; {int n;n=5;} cout<<n; system("pause"); return 0; } The compiler tells me that n is undeclared. Then I tried making it static, but again, the compiler tells me that it is undeclared. Doesn't a variable declated static have program scope? If not, how do I use n in this program? 回答1: You're confusing scope with lifetime. Static variables have a lifetime equal to the program's lifetime, but they still follow

Local/static variable scope in C++

风流意气都作罢 提交于 2019-12-21 06:18:14
问题 If I write something like this: #include <iostream> int main() { using namespace std; {int n;n=5;} cout<<n; system("pause"); return 0; } The compiler tells me that n is undeclared. Then I tried making it static, but again, the compiler tells me that it is undeclared. Doesn't a variable declated static have program scope? If not, how do I use n in this program? 回答1: You're confusing scope with lifetime. Static variables have a lifetime equal to the program's lifetime, but they still follow

Variable scope inside lambda

冷暖自知 提交于 2019-12-21 04:57:10
问题 Why does this code print "d, d, d, d", and not "a, b, c, d"? How can I modify it to print "a, b, c, d"? cons = [] for i in ['a', 'b', 'c', 'd']: cons.append(lambda: i) print ', '.join([fn() for fn in cons]) 回答1: Oddly enough, this is not a variable scope problem, but a quesiton of the semantics of python's for loop (and of python's variables). As you expect, i inside your lambda correctly refers to the variable i in the nearest enclosing scope. So far, so good. However, you are expecting this