scope

Why does this work in javascript?

拈花ヽ惹草 提交于 2019-12-07 07:05:27
问题 Just now,I saw some code like this: if(condition){ var xx='sss'; } //do something if(condition){ console.info(xx); } Now, I just wonder why the second if statement work? How can it access the xx variable since it is a local variable defined in another if statement? 回答1: var in JavaScript is scoped to the containing execution context (e.g., the whole function's scope, or the whole global scope if the var is at global scope), not the block. JavaScript doesn't (yet) have block scope (ECMAScript6

Django templatetag scope forcing me to do extra queries

你说的曾经没有我的故事 提交于 2019-12-07 06:52:06
问题 The problem is that if I call a templatetag into a block and it fills me a variiable with the usual context[varname]=something, then if I need that variable into another block, I have to call the templatetag again. This for me means extra db queries, which is really something I'm trying to avoid. This templatetag is called in a base template which is extended by many other templates, so I can't just change all the views to pass something to the context, it makes no sense (WET principle?) Even

does passing a method of one object to another object keep the first object alive?

心已入冬 提交于 2019-12-07 06:27:12
问题 Suppose I have three objects: 'a', 'b' and 'c'. Object 'a' and 'c' are long-lived, statically referenced service singletons. Object 'b' is short-lived, i.e. no static references keep it alive. Now suppose object 'a' creates an instance of object 'b' in the scope of one of its methods, e.g. B b = new B(); Further suppose that the class B looks something like this: public B () { C.ActionList.Add ( SomeMethod ); } void SomeMethod () { ... } Now, how long does object 'b' live? My presumption is

How to create a scope to get the last ten transactions with rails3

孤人 提交于 2019-12-07 06:27:00
问题 Trying to add a scope to my transactions model to return the last 10 transactions by created_at 回答1: scope :most_recent, order(created_at: :desc).limit(10) 回答2: Use scopes # Ruby 1.8 style scope :recent, lambda { |num| order('created_at DESC').limit(num) } # Ruby 1.9/2.0 style scope :recent, ->(num) { order('created_at DESC').limit(num) } Example Usage: <% Organization.recent(10).each do |organization| %> <li><% link_to organization.name, organization %></li> <% end %> 回答3: If you want to do

something like stackbased objects in c++ for javascript

◇◆丶佛笑我妖孽 提交于 2019-12-07 05:53:59
问题 Looking for a construct in javascript which works like the destructor in stackbased or local object in c++, e.g. #include <stdio.h> class M { public: int cnt; M() {cnt=0;} void inc() {cnt++;} ~M() {printf ("Count is %d\n", cnt);} }; ... {M m; ... m.inc (); ... m.inc (); } // here the destructor of m will printf "Count is 2"); so this means I am looking for a construct which does an action when its scope is ending (when it "goes out of scope"). It should be robust in the way that it does not

Dim vs Private/Public

∥☆過路亽.° 提交于 2019-12-07 04:54:30
问题 At the head of a module, I wish to declare some global variables for use in various subs/functions. What is the difference between Dim x as string and Private x as string / Public x as string , and when would I use one over the other? 回答1: Private and public control the scope of the variable or object you're declaring. Private will only allow members of the relative module/class/whatever to access the instance public will allow anything in the same scope as the module/class/whatever to access

Closures in a class scope

青春壹個敷衍的年華 提交于 2019-12-07 04:14:26
问题 From my understanding, function and class scopes behave pretty much the same: >>> def x(): ... a = 123 ... print (locals()) ... >>> x() {'a': 123} >>> class x(): ... a = 123 ... print (locals()) ... {'a': 123, '__module__': '__main__'} When, however, I define a closure, behaviors are different. A function simply returns the local binding, as expected: >>> def x(): ... a = 123 ... t = lambda: a ... return t ... >>> x()() 123 whereas in a class the binding appears to be lost: >>> class x(): ...

PHP isset($this) and using the same object method in a static and object context

孤者浪人 提交于 2019-12-07 04:10:02
问题 I'm working on a class which needs to be accessible via static function calls as well as object methods. One thing I have found is that I'm duplicating logic across multiple functions. Simplified example: class Configurable{ protected $configurations = array(); protected static $static_configurations = array(); public function configure($name, $value){ // ...lots of validation logic... $this->configurations[$name] = $value; } public static function static_configure($name, $value){ // ...lots

Why does tensorflow/keras choke when I try to fit multiple models in parallel?

安稳与你 提交于 2019-12-07 03:39:00
问题 I'm trying to fit a finite mixture model, with the mixture models for each class being neural networks. It'd be super-useful for me to be able to be able to parallelize, because keras doesn't max out all of the available cores on my laptop, let alone a large cluster. But when I try to set different learning rates for different models inside of a parallel foreach loop the whole thing chokes. What is going on? I suspect that it has something to do with scope -- the workers aren't running on

What type of scope does Haskell use?

感情迁移 提交于 2019-12-07 03:26:52
问题 I'm trying to figure out if Haskell uses dynamic or static scoping. I realize that, for example, if you define: let x = 10 then define the function let square x = x*x You have 2 different "x's", and does that mean it is dynamically scoped? If not, what scoping does it use, and why? Also, can Haskell variables have aliases (a different name for the same memory location/value)? Thanks. 回答1: There are some things wrong in your statements... There are no mutable variables in Haskell just