scope

Using json_encode on objects in PHP (regardless of scope)

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-27 17:00:36
问题 I'm trying to output lists of objects as json and would like to know if there's a way to make objects usable to json_encode ? The code I've got looks something like $related = $user->getRelatedUsers(); echo json_encode($related); Right now, I'm just iterating through the array of users and individually exporting them into arrays for json_encode to turn into usable json for me. I've already tried making the objects iterable, but json_encode just seems to skip them anyway. edit : here's the var

Global JavaScript Variable Scope: Why doesn't this work?

我怕爱的太早我们不能终老 提交于 2019-12-27 15:41:12
问题 So I'm playing around with JavaScript and came across what I think to be an oddity. Is anyone able to explain the following? (i've included the alerted values as comments) Why is the first alert(msg) inside foo() returning undefined and not outside ? var msg = 'outside'; function foo() { alert(msg); // undefined var msg = 'inside'; alert(msg); // inside } foo(); alert(msg); // outside Considering these both work fine: var msg = 'outside'; function foo() { alert(msg); // outside } alert(msg);

Static variables in C++

痞子三分冷 提交于 2019-12-27 11:52:51
问题 I would like to know what is the difference between static variables in a header file vs declared in a class. When static variable is declared in a header file is its scope limited to .h file or across all units. Also generally static variable is initialized in .cpp file when declared in a class right? So that does mean static variable scope is limited to 2 compilation units? 回答1: Excuse me when I answer your questions out-of-order, it makes it easier to understand this way. When static

Why is window (and unsafeWindow) not the same from a userscript as from a <script> tag?

主宰稳场 提交于 2019-12-27 11:04:38
问题 I was facing an issue while developing this small userscript. When I wanted to block every XMLHttpRequest from the running website with my script, nothing was happening (at least with Chrome): function main() { // Override XHR.open with a custom function window.XMLHttpRequest.prototype.open = function() { // Nothing... so it's supposed to block every xhr.open() call } } main(); Same thing when replacing window by unsafeWindow . However, when I used this little trick, everything worked like a

Variable Scope Example Explanation

北慕城南 提交于 2019-12-25 18:19:44
问题 public class Arcane { static int x; int y; public Arcane(int x) { int y = x + 1; this.y = y; } public void increment() { y += x; } public void print() { System.out.println(y); } public static void main(String[] args) { x = 5; { int x = 2; Arcane t = new Arcane(x); t.increment(); t.print(); } } } It is in my understanding that the program prints out 8 but I cannot figure out why. I tried plugging in x=5. 回答1: First you put 5 to static variable x: x = 5; Then you create yet another x valid in

Problem with class template specialisations

人走茶凉 提交于 2019-12-25 16:24:13
问题 I'm trying to port some code from VC9 to G++, however Ive run into a problem with template specialisations apparently not being allowed for class members. The following code is an example of these errors for the getValue specialisations of the class methods. In all cases the error is "error: explicit specialization in non-namespace scope class ... " template<typename T> T getValue(const_iterator key)const { try{return boost::lexical_cast<T>(key->second);} catch(boost::bad_lexical_cast &e) {

What way (if any) can I get the reference to anonymous function's closure in javascript?

六月ゝ 毕业季﹏ 提交于 2019-12-25 14:22:44
问题 Let us consider the workable code: var storage = {}; (function() { function internalMethod1() { ...; return; } function internalMethod2() { ...; return; } storage.storedMethod = internalMethod1; })(); storage.storedMethod(); Is there any way to call internalMethod2 , if it is not called in internalMethod1 ? In other words, can I access an anonymous closure from outside, if I have access only to one of its functions? 回答1: can I access an anonymous closure from outside? No. Scopes are private

What way (if any) can I get the reference to anonymous function's closure in javascript?

 ̄綄美尐妖づ 提交于 2019-12-25 14:22:14
问题 Let us consider the workable code: var storage = {}; (function() { function internalMethod1() { ...; return; } function internalMethod2() { ...; return; } storage.storedMethod = internalMethod1; })(); storage.storedMethod(); Is there any way to call internalMethod2 , if it is not called in internalMethod1 ? In other words, can I access an anonymous closure from outside, if I have access only to one of its functions? 回答1: can I access an anonymous closure from outside? No. Scopes are private

How to Bind dynamic data in textbox when user selects row in a Grid

馋奶兔 提交于 2019-12-25 12:25:31
问题 I am using AngularJS - 1.0.6 version. I have created sample application where I want data to be populated in the textbox when user selects row in a grid. I have a directive which will render textbox and bind data with ng-model property. The model which binds the data with ng-model is dynamic. for e.g. - input.attr('ng-model', 'model["' + d.id.toLowerCase() + '"]'); I need to use Angular.copy method to copy selected Items data into another model object. $scope.model = angular.copy($scope

Ruby local variable scope

僤鯓⒐⒋嵵緔 提交于 2019-12-25 09:27:59
问题 I'm struggling with variable scope in ruby. I was under the impression that local variables were accessible by methods below them. Looking at the following code however I'm getting an undefined variable error. a = Array.new def testing() a.push("test") end testing Using global variables it works just fine, how can I avoid using global variables? 回答1: There isn't much to say here except that local variables in Ruby are only accessible in the scope in which they are defined and any blocks