scope

How does the JS scope of these blocks work?

徘徊边缘 提交于 2019-12-10 17:23:03
问题 Can anyone explain why the following produces 1,2 and the other produces 5? Should they not both produce 5? //produces 1,2 (function () { var a = [5]; function bar() { if (!a) { var a = [1, 2]; } console.log(a.join()); } bar(); })(); Based on reading some articles about JS closure, I expect them both to produce 5. Can't seem to find an article anywhere that would give some insight as to why the first block produces otherwise. //produces 5 (function () { var a = [5]; function bar() { if (a) {

Call class method from inside array_map anonymous function

蓝咒 提交于 2019-12-10 17:19:08
问题 I am trying to call one of my object's methods from within an array_map anonymous function. So far I am receiving the expected error of: Fatal error: Using $this when not in object context in... I know why I am getting this error, I just don't know a way to achieve what I am trying to... Does anybody have any suggestions? Here is my current code: // Loop through the data and ensure the numbers are formatted correctly array_map(function($value){ return $this->some_method($value,'value',false);

javascript hoisting for global variable

☆樱花仙子☆ 提交于 2019-12-10 17:14:42
问题 I was wondering how javascript hoisting works for global variable. Let's say I have following code snippet: var a = 5; function print(){ console.warn("a",a,b); var a = 10; b=5; console.warn("a",a); } print(); In this case I am getting error "b is not defined". I wonder why Javascript hoisting is not working for global variable. I tried to look for this but getting results only for variable hoisting. Any thoughts?? 回答1: var statements are hoisted. function declarations are hoisted. Assignments

Access inner variables from external functions in javascript

℡╲_俬逩灬. 提交于 2019-12-10 16:48:54
问题 Is it possible to access an inner variable from an external function like this example? function a(f) { var c = 'test'; f(); } a(function() { alert(c); //at this point, c should = "test" }); 回答1: No, that won't work. What matters is where (lexically) a function is defined , not where it's invoked. When figuring out what (if anything) "c" refers to, the language looks in the local scope, then in the next scope out based on the definition of the function . Thus if that invocation of "a" took

Derived template-class access to base-class member-data

时间秒杀一切 提交于 2019-12-10 16:33:20
问题 This question is a furtherance of the one asked in this thread. Using the following class definitions: template <class T> class Foo { public: Foo (const foo_arg_t foo_arg) : _foo_arg(foo_arg) { /* do something for foo */ } T Foo_T; // either a TypeA or a TypeB - TBD foo_arg_t _foo_arg; }; template <class T> class Bar : public Foo<T> { public: Bar (const foo_arg_t bar_arg, const a_arg_t a_arg) : Foo<T>(bar_arg) // base-class initializer { Foo<T>::Foo_T = T(a_arg); } Bar (const foo_arg_t bar

Access Codeigniter data variables in function inside a view

感情迁移 提交于 2019-12-10 16:32:34
问题 I have a function in one of my views, and I want to access one of the variables available to the view through CodeIgniter's data array. For example; in my controller I have this: $this->load->view('someview', array( 'info' => 'some info' )); Now, within my view, I have a function, and I want to be able to access the variable $info from within that function scope. Is that possible? 回答1: in your controller: $GLOBALS['var_available_in_function'] = $value; Your function: function get_var() {

Autofac and Web API scopes

泪湿孤枕 提交于 2019-12-10 16:27:56
问题 Earlier i saw this: .InstancePerApiRequest(); Now i only got this: .InstancePerHttpRequest(); Did autofac removed the API-scope? I have the reference Autofac.Integration.WebApi but this extension is not available. What is the difference between InstancePerHttpRequest and InstancePerApiRequest? 回答1: InstancePerApiRequest is part of the Web API integration, and InstancePerHttpRequest is part of the MVC integration. These both actually apply the same tag to the lifetime scope. That was done

Maintain scope when loading script with jQuery

删除回忆录丶 提交于 2019-12-10 16:22:22
问题 Say I have a file 'test.js' with the following contents: var test = 'something'; Then I have a primary script that needs to load up test.js to grab the test variable. Obviously this works: $.ajax({dataType: "script", cache: true, url: 'test.js'}); The issue is that the variable test exists in the global scope. I'm curious if there's a way to add it into an object and keep it out of the global scope. Something like: function scriptloader() { this.grabscript = function() { return $.ajax(

The only option is to include that block of code into each of my functions?

爷,独闯天下 提交于 2019-12-10 16:17:45
问题 Several of my functions require the UniversalXPConnect privilege to be enabled. netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect'); So, my functions look like this: function oneOfMyFunctions() { netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect'); // ... } Actually, I also try to catch the exception when the privilege is denied. Looks as follows: try { netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect'); // ... } catch (e) { // .

Nesting reference type object in inner scope has no effect on garbage collection: True or False?

青春壹個敷衍的年華 提交于 2019-12-10 15:43:41
问题 I have been reading the Garbage Collection chapter from Jeffrey Richter's fine book, "CLR via C#". There, he illustrated an example of how the GC conceptually works (how roots are marked) by referring to a disassembly listing of native code emitted from the JIT compiler. From that example, it occurred to me that nesting reference types in scope seems to have ZERO effect on expediting the garbage collection of the nested variable. I wonder if I am understanding this correctly. In any case,