Can someone please explain how an object returned from a function has access to the function's properties when it is no longer part of the function and it is returned. for example, look at the code below.  const arrayList = [1,2,3,4,5,6,7,8];  function one(array) {     let i = 0;      return {         next : function () {             return {                 value : array[i++]             }         }     } }  const two = one(arrayList); // variable two will now have the object returned from function one  console.log(two.next()); // but in here when the next() method is called  console.log(two.next()); // how it can have access to 'i' variable when  console.log(two.next()); // it is no longer part of the function one   This will output ...  {value: 1} {value: 2} {value: 3}   What I don't understand here is, when two.next() is called, how can the next() function have access to 'i' variable from function one.
1086
How would you explain JavaScript closures to someone with a knowledge of the concepts they consist of (for example functions, variables and the like), but does not understand closures themselves?  I have seen  given on Wikipedia, but unfortunately it did not help.
18599
I want to return two objects from a Java method and was wondering what could be a good way of doing so?  The possible ways I can think of are: return a HashMap (since the two Objects are related) or return an ArrayList of Object objects.  To be more precise, the two objects I want to return are (a) List of objects and (b) comma separated names of the same.  I want to return these two Objects from one method because I dont want to iterate through the list of objects to get the comma separated names (which I can do in the same loop in this method).  Somehow, returning a HashMap does not look a very elegant way of doing so.
14541
I have a pretty good understanding of Javascript, except that I can't figure out a nice way to set the "this" variable.  Consider:  var myFunction = function(){     alert(this.foo_variable); }  var someObj = document.body; //using body as example object someObj.foo_variable = "hi"; //set foo_variable so it alerts  var old_fn = someObj.fn;   //store old value someObj.fn = myFunction;   //bind to someObj so "this" keyword works someObj.fn();               someObj.fn = old_fn;       //restore old value   Is there a way to do this without the last 4 lines?  It's rather annoying...  I've tried   binding an anonymous function, which I thought was beautiful and clever, but to no avail:  var myFunction = function(){     alert(this.foo_variable); }  var someObj = document.body;        //using body as example object someObj.foo_variable = "hi";        //set foo_variable so it alerts someObj.(function(){ fn(); })();    //fail.   Obviously, passing the variable into myFunction is an option... but that's not the point of this question.  Thanks.
106919
I need to call a const function from a non-const object. See example  struct IProcess {    virtual bool doSomeWork() const = 0L; }; class Foo : public IProcess {       virtual bool doSomeWork() const {     ...   } };  class Bar { public:    const IProcess&amp; getProcess() const {return ...;}    IProcess&amp; getProcess() {return ...;}     void doOtherWork {     getProcess().doSomeWork();           } };   Calling   getProcess().doSomeWork();   will always results in a call to  IProcess&amp; getProcess()   Is there another way to call  const IProcess&amp; getProcess() const    from a non constant member function? I have so far used  const_cast&lt;const Bar*&gt;(this)-&gt;getProcess().doSomeWork();   which does the trick but seems overly complicated.    Edit: I should mention that code is being refactored and eventually only one function will remain.  const IProcess&amp; getProcess() const    However, currently there is a side effect and the const call may return a different instance of IProcess some of the time.  Please keep on topic.
105996
When I'm debugging, I often have to deal with methods that does not use an intermediate variable to store the return value :     private int myMethod_1()    {       return 12;    }     private int myMethod_2()    {       return someCall( someValue );    }   Well, in order to recreate a bug, I often have to change values on the fly. Here, I could want to see what happens when myMethode_1 return zero. Same thing for myMethod_2.  Is there a way to do that without modifying the code ? What I would like to do, it's to place a breakpoint on the return line (or on the closing bracket) and type-in a new return value.  Note : I'm using Visual Studio 2005 and Visual Studio 2008.  Thank !
38274
I've heard from a variety of places that global variables are inherently nasty and evil, but when doing some non-object oriented Javascript, I can't see how to avoid them. Say I have a function which generates a number using a complex algorithm using random numbers and stuff, but I need to keep using that particular number in some other function which is a callback or something and so can't be part of the same function.   If the originally generated number is a local variable, it won't be accessible from, there. If the functions were object methods, I could make the number a property but they're not and it seems somewhat overcomplicated to change the whole program structure to do this. Is a global variable really so bad?
69514
In Python one can get a dictionary of all local and global variables in the current scope with the built-in functions locals() and globals().  Is there some equivalent way of doing this in Javascript?  For instance, I would like to do something like the following:  var foo = function(){ alert('foo'); }; var bar = function(){ alert('bar'); };  var s = 'foo'; locals()[s](); // alerts 'foo'   Is this at all possible, or should I just be using a local object for the lookup?
12542
Are there good reasons why it's a better practice to have only one return statement in a function?   Or is it okay to return from a function as soon as it is logically correct to do so, meaning there may be many return statements in the function?
2818
I know in JavaScript, objects double as hashes, but I have been unable to find a built-in function to get the keys: var h = {a:'b', c:'d'};  I want something like var k = h.keys() ; // k = ['a', 'c'];  It is simple to write a function myself to iterate over the items and add the keys to an array that I return, but is there a standard cleaner way to do that? I keep feeling it must be a simple built in function that I missed but I can't find it!
106335
What does the following mean?  Class.Function(variable := 1 + 1)   What is this operator called, and what does it do?
96862
Aura Action javascript object has setCallback function that we use to communicate with the server. The first parameter of the function is scope. Aura documentation has the following explanation:  Scope : Object The scope in which the function is executed. You almost always want to set scope to the keyword this.  Experimenting with this keyword inside the component results in undefined when used inside controller.js and the helper object instance when used inside helper.js. Does someone know what the scope parameter is affecting? Using this feels wrong as it can be almost anything depending on the place in code the setCallback is executed from. P.S. currently I have problems with private attribute visibility inside the action callback function so I am trying to eliminate all possible causes.
100509
I want to know about the setter and getter. I know that it is a properties of declaring variable but some time I get confused. Presently I am using a sample of code that is below:  public with sharing class hello {     public String temp{set;get;}      public hello(){         temp = 'first' ;     }      public String gettemp(){         return 'second' ;     } }   So I want to know that when I use temp on VFP than what value will show on page, what is reason for that, and what is the difference between temp and gettemp?  Thanks Ashlekh Gera
699
I am trying to return an array Data Member from one smaller 2D Array Object, and trying to insert the array into a larger 2D array object. But when attempting this, I came into two problems.  First problem is that I want to return the name of the 2D array, but I do not know how to properly syntax to return 2D Array name.  This is what my 2D Array data member looks like   private: int pieceArray[4][4]; // 2D Smaller Array   and I want to return this array into a function, but this one causes a compiler error:   int Piece::returnPiece() {     return pieceArray; //not vaild     // return the 2D array name }   I tired using this return type and it worked:   int Piece::returnPiece() {     return pieceArray[4][4]; }   But I am unsure if this is what I want, as I want to return the array and all of it's content.  The other problem is the InsertArray() function, where I would put the returnPiece() function in the InsertArray()'s argument.  The problem with the InsertArray() is the argument, heres the code for it:   void Grid::InsertArray( int arr[4][4] ) //Compiler accepts, but does not work {     for(int i = 0; i &lt x_ROWS ; ++i)     {          for (int j = 0; j &lt y_COLUMNS ; ++j)          {              squares[i][j] = arr[i][j];          }     } }   The problem with this is that it does not accept my returnPiece(), and if i remove the "[4][4]", my compiler does not accept.  Mostly all these are syntax errors, but how do I solve these problems?   Returning the whole pieceArray in returnPiece() The correct syntax for the argument in InsertArray() The argument of InsertArray() accepting the returnPiece()   These 3 are the major problems that I need help with, and had the same problem when I attempt to use the pointer pointer method. Does anyone know how to solve these 3 problems?
59216
There are two different ways to create an empty object in JavaScript:  var objectA = {} var objectB = new Object()   Is there any difference in how the script engine handles them? Is there any reason to use one over the other?  Similarly it is also possible to create an empty array using different syntax:  var arrayA = [] var arrayB = new Array()
36511
I understand passing in a function to another function as a callback and having it execute, but I'm not understanding the best implementation to do that. I'm looking for a very basic example, like this:  var myCallBackExample = {     myFirstFunction : function( param1, param2, callback ) {         // Do something with param1 and param2.         if ( arguments.length == 3 ) {             // Execute callback function.             // What is the "best" way to do this?         }     },     mySecondFunction : function() {         myFirstFunction( false, true, function() {             // When this anonymous function is called, execute it.         });     } };   In myFirstFunction, if I do return new callback(), then it works and executes the anonymous function, but that doesn't seem like the correct approach to me.
41914
Consider:  int testfunc1 (const int a) {   return a; }  int testfunc2 (int const a) {   return a; }   Are these two functions the same in every aspect or is there a difference?  I'm interested in an answer for the C language, but if there is something interesting in the C++ language, I'd like to know as well.
118197
In C++, is the return type considered part of the function signature? and no overloading is allowed with just return type modified.