scope

Scoping: Local vs Var

烂漫一生 提交于 2019-12-19 07:23:06
问题 I'm new to CF so this may be a basic question. But I've heard I should use local for objects inside functions due to how scoping works in CF. But what about 'var'? Is var the same as using local? e.g. function MyFunction() { local.obj = {}; } Is this the same as: function MyFunction() { var obj = {}; } If they aren't the same, what is the difference between them? And when should I be using either of them? 回答1: They are very similar, but not exactly the same. Both only exist inside of a

Using $1, $2, etc. global variables inside method definition

限于喜欢 提交于 2019-12-19 06:05:07
问题 Given the following two pieces of code: def hello(z) "hello".gsub(/(o)/, &z) end z = proc {|m| p $1} hello(z) # prints: nil def hello z = proc {|m| p $1} "hello".gsub(/(o)/, &z) end hello # prints: "o" Why are the outputs of these two pieces of code different? Is there a way to pass a block to gsub from outside of the method definition so that the variables $1 , $2 would be evaluated in the same way as if the block was given inside the method definition? 回答1: Why the output is different? A

Javascript outer scope variable access

这一生的挚爱 提交于 2019-12-19 06:04:21
问题 OperationSelector = function(selectElement) { this.selectElement = selectElement; } OperationSelector.prototype.populateSelectWithData = function(xmlData) { $(xmlData).find('operation').each(function() { var operation = $(this); selectElement.append('<option>' + operation.attr("title") + '</option>'); }); } How could I access OperationSelector.selectElement in iteration block ? 回答1: Assign it to a local variable in the function scope before your iteration function. Then you can reference it

Javascript outer scope variable access

心不动则不痛 提交于 2019-12-19 06:04:16
问题 OperationSelector = function(selectElement) { this.selectElement = selectElement; } OperationSelector.prototype.populateSelectWithData = function(xmlData) { $(xmlData).find('operation').each(function() { var operation = $(this); selectElement.append('<option>' + operation.attr("title") + '</option>'); }); } How could I access OperationSelector.selectElement in iteration block ? 回答1: Assign it to a local variable in the function scope before your iteration function. Then you can reference it

Using $1, $2, etc. global variables inside method definition

我的未来我决定 提交于 2019-12-19 06:04:07
问题 Given the following two pieces of code: def hello(z) "hello".gsub(/(o)/, &z) end z = proc {|m| p $1} hello(z) # prints: nil def hello z = proc {|m| p $1} "hello".gsub(/(o)/, &z) end hello # prints: "o" Why are the outputs of these two pieces of code different? Is there a way to pass a block to gsub from outside of the method definition so that the variables $1 , $2 would be evaluated in the same way as if the block was given inside the method definition? 回答1: Why the output is different? A

How to pass 'this' into a setTimeout callback

馋奶兔 提交于 2019-12-19 05:55:34
问题 css .item { display: none; } html <div> <div class="item">machin</div> <div class="item">chose</div> <div class="item">chouette</div> <div class="item">prout</div> </div> I'm using jQuery and I'd like to make each .item appearing after a random little timer like: javascript $('.item').each(function () { itm = $(this); setTimeout(function () { itm.fadeIn(1000); }, Math.floor(Math.random() * 1000)); }) Here itm will always contain the last item because the function is evaluated after all

How to pass 'this' into a setTimeout callback

坚强是说给别人听的谎言 提交于 2019-12-19 05:55:14
问题 css .item { display: none; } html <div> <div class="item">machin</div> <div class="item">chose</div> <div class="item">chouette</div> <div class="item">prout</div> </div> I'm using jQuery and I'd like to make each .item appearing after a random little timer like: javascript $('.item').each(function () { itm = $(this); setTimeout(function () { itm.fadeIn(1000); }, Math.floor(Math.random() * 1000)); }) Here itm will always contain the last item because the function is evaluated after all

Ninject InRequestScope fallback to InThreadScope

北城余情 提交于 2019-12-19 05:42:40
问题 In my MVC3 project I've setup my kernel to Ninject the Entityframework context on a InRequestScope basis, this works perfect, but I have a background runner that does some workflow management. It fires up a new thread each 5 minutes and I Ninject my dependencies into this thread, If I change the scope to InThreadScipe the Dispose method is fired, but If i change it back to InRequestScope the Dispose method wont fire. Is there a way of fallbacking to InThreadScope if InRequestScope isnt

require_once to global scope within a function

那年仲夏 提交于 2019-12-19 05:18:39
问题 It seems that if require_once is called within function, the included file doesn't extend the global variable scope. How to require_once a file to global scope from within a function? What I'm trying to do is some dynamic module loader: function projects_init() { ... foreach ($projects as $p) { require_once($p['PHPFile']); $init_func = $p['init']; if ($init_func) $init_func(); } } If it is not possible to use require_once that way, what is the simplest solution for this? (Please no heavy

Try-with-resources scope of resource

≯℡__Kan透↙ 提交于 2019-12-19 05:16:08
问题 In the try -with-resources construct of Java 7 , I can declare a resource in the try statement, and it will be closed automatically when it goes out of scope. However, I don't find any indication of the scope of the resource made available. Specifically, is it available in the catch / finally blocks of the try block where it is declared? I tried the following in Eclipse Kepler , but it's giving a mixed impression: Resource variable is made available by Content Assist (Code Completion) : Quick