JavaScript Assigning a Callback to an Object inside an Object

Answer to comment
No. I'm not sure that I could explain well, but still try. So, this is function's keyword, its value depends in which context it's used and it also has some differences between strict mode and non-strict mode.

If it's used in Global context this will refer to window object in strict and non-strict mode.
Strict mode:

"use strict";
console.log(this); // window

Non-Strict mode:

console.log(this); // window

If it's used in Function context the value maybe differ from where this function is called, if it's called from Global context its will refer to window object in non-strict mode, and undefined in strict mode.

Strict mode:

(function() {
    "use strict";
    console.log(this); // undefined
}());

Non-Strict mode:

(function() {
    console.log(this); // window
}());

When a function is called as a method of an object, its this is set to the object the method is called on.
So, in your case:

var Foo = {
    /* ... */
    bar: function(data) {
        // this refer to `Foo`
        this.variables.forEach(function(elem) {
            // but, here `this` refer to window in non-strict mode and undefined in strict mode
            if(data[elem.name] == 1)
                elem.callback();
        });

    }/*, ... */
};

and, if your object Foo is declared in global context its will refer to window object in strict mode and in non-strict mode too:

var Foo = {
    variables: [
        {name: "x", callback: this.funX}, // this === window 
        {name: "y", callback: this.funY}, // same
        {name: "z", callback: this.funZ}  // ...
    ]/*, ... */
};

but, if it's declared in function scope and function called from global context:

function myFunction() {
    var Foo = { 
        variables: [
            // this will refer to window in non-strict mode and undefined in strict mode
            {name: "x", callback: this.funX}/*, ... */            
        ]/*, ... */
    }; 
}
myFunction();  

but, if it's a object method and there's no difference in which context is object declared:

var myObj = { 
    myMethod : myFunction  // now this will refer to myObj
};

Since, you can invoke function as a constructor with new operator, in such case this will refer to an instance of that constructor (class) and there is no difference between strict or non-strict mode.

function MyClass(v) {
    this.myProp = v; // this will refer to instances of MyClass
}
var myClass1 = new MyClass("some value");
var myClass2 = new MyClass("other value");

console.log(myClass1.myProp); // "some value";
console.log(myClass2.myProp); // "other value";

Also, you can bind explicity this with Function.prototype.call, Function.prototype.apply and with Funciton.prototype.bind

var myObj = {
    x: 5,
    y: 6
};

function sum() {
    return this.x + this.y;
}

console.log(sum.call(myObj)); // 11
console.log(sum.apply(myObj)); // 11
console.log(sum.bind(myObj)()); // 11
Answer:1

I am trying to json encode a variable to have the value of undefined, as in the javascript keyword undefined. I tried to google this, but the only results that show up are about people getting errors ...

I am trying to json encode a variable to have the value of undefined, as in the javascript keyword undefined. I tried to google this, but the only results that show up are about people getting errors ...

I am using the bootstrap popover for a drop down menu. Is there any way I could disable the popover function either with css media queries or by jquery on page resize and make the menu visible and ...

I am using the bootstrap popover for a drop down menu. Is there any way I could disable the popover function either with css media queries or by jquery on page resize and make the menu visible and ...

  1. disable bootstrap popover
  2. bootstrap disable popover on mobile
  3. ngx-bootstrap disable popover
  4. bootstrap popover disable click
  5. bootstrap popover disable arrow
  6. bootstrap popover disable title

Have looked at many examples but cant seem to get to make an nested array to store some data nicely. How can I get the following code to work? It gives me an error now: var shipdata = []; shipdata['...

Have looked at many examples but cant seem to get to make an nested array to store some data nicely. How can I get the following code to work? It gives me an error now: var shipdata = []; shipdata['...

  1. creating nested array javascript
  2. create nested array php
  3. create nested array python
  4. create nested array
  5. create nested array js
  6. create nested array java
  7. create nested array in jquery
  8. create nested array c#
  9. create nested array laravel
  10. create nested array json
  11. create nested array in angularjs
  12. create nested array typescript
  13. create nested array arduinojson
  14. create nested array of objects
  15. create nested arrays in json objects
  16. create bidimensional array python
  17. create bidimensional array js
  18. creating a nested array from items with parent ids
  19. create a nested array recursively in javascript
  20. create nested json array in java

I am Working on Codeigniter and am getting a problem while inserting values in database via ajax. Problem occur when sql query throws an error. This is My Controller code:- public function ...

I am Working on Codeigniter and am getting a problem while inserting values in database via ajax. Problem occur when sql query throws an error. This is My Controller code:- public function ...

  1. jquery ajax data shows object object