JavaScript Javascript callbacks, refering to own object

Below is a very simplified version of my problem. Without changing the definition of object "o" and without using the name "obj", how can i refer to obj inside foo.

var obj = {
    foo: function () {
        alert(this.greet); // => undefined, obj.greet works but not feasible for my case
    },
    greet: "hi"
};

var o = {
    m: obj.foo
};

o.m();
Answer:1

Create a new function that calls the old function with the correct context. The bind method will do this for you in sufficiently modern browsers.

var o = {
    m: obj.foo.bind(obj)
};

If you want to support legacy browsers:

var o = {
    m: (function (context) { 
        return function () { 
            context.foo(); 
        };
    }(obj))
};
Answer:2

Ok. So I'm trying to re create this really cool looking button that I found. The example is at http://gyazo.com/cafe378e8340d71c341b7c52857b2689 See how it is first invisible but now it's faded a ...

Ok. So I'm trying to re create this really cool looking button that I found. The example is at http://gyazo.com/cafe378e8340d71c341b7c52857b2689 See how it is first invisible but now it's faded a ...

So I've used bootstrap accordion to hide/unhide info. I needed the wording to change from 'See more' to 'See less' continuously. I've implemented this as you can see in the bootply As you will see,...

So I've used bootstrap accordion to hide/unhide info. I needed the wording to change from 'See more' to 'See less' continuously. I've implemented this as you can see in the bootply As you will see,...

I'm using the following code to detect multiple keys on a keypress event: var down = []; $(document).keydown(function (e) { down[e.keyCode] = true; }).keyup(function (e) { if (down[17] &...

I'm using the following code to detect multiple keys on a keypress event: var down = []; $(document).keydown(function (e) { down[e.keyCode] = true; }).keyup(function (e) { if (down[17] &...

  1. prevent spaces input
  2. prevent spaces in input field
  3. prevent spaces in input field angular
  4. prevent spaces in input field html
  5. javascript prevent spaces in input
  6. angular prevent spaces in input
  7. jquery prevent space input

I need to chagen my JSON output which is: [ { masterCardNo: 90363.01 }, { masterCardNo: 90363.02 }, { masterCardNo: 90363004 }, { masterCardNo: 90363004 }, { ...

I need to chagen my JSON output which is: [ { masterCardNo: 90363.01 }, { masterCardNo: 90363.02 }, { masterCardNo: 90363004 }, { masterCardNo: 90363004 }, { ...