-3

I find myself wanting to re-define "this" many times through the day. Could someone please tell me how to achieve this or why I cannot?

For example:

this = $('div')[0];

Why i would like to do this: working with 'this' is more natural then passing a reference through a function call. 'this' should refer to the calling object inside of a function for example:

var my_div = $('#some_div');
my_div.click(function(){
    console.log(this);
});

What gets sent to the log is a reference to the window not to the div that is calling the function.

Every onchange event handler I write is forced to take 'this' as a parameter in order to maintain reference to the calling object. To me this seems broken.

josh123a123
  • 1,723
  • 1
  • 11
  • 20
  • Please provide more information, your question is really unclear – Magicprog.fr Jun 26 '15 at 14:59
  • Why would you need to do this? The `this` keyword has specific meanings depending on how you call the function it's in. It's better to learn what they are. – slebetman Jun 26 '15 at 14:59
  • Have a read of [this question/answer](http://stackoverflow.com/questions/3127429/how-does-the-this-keyword-work). It will probably cover everything you ever wanted to know... – James Thorpe Jun 26 '15 at 14:59
  • Instead you can use: `$this = this;` – Praveen Kumar Purushothaman Jun 26 '15 at 14:59
  • 1
    Sounds like you really should be using bind, call, or apply. – epascarello Jun 26 '15 at 15:02
  • *"What gets sent to the log is a reference to the window not to the div that is calling the function."* Not true. In your example, `this` will refer to the DOM element with ID `some_div`. I think the problem is rather that you don't understand how `this` works. – Felix Kling Jun 27 '15 at 01:22

3 Answers3

3

You can use .call(context[, params])and .apply(context[, arguments]) to do this. For example:

function move(x, y) {
    this.style.left = x + "px";
    this.style.top = y + "px";
};

// now this in move refers to the div element
move.call($('div')[0], 100, 200);

But you can't just overwrite this.

Sebastian Nette
  • 5,878
  • 2
  • 15
  • 17
1

Depending on what you are trying to do you can use javascript's bind. It modifies the context of the this keyword on function calls. You can take a look at https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/bind for more information but here's a simple example.

var obj1 = {
    x: 5,
    getX: function() {
        return this.x;
    }
};

var obj2 = {
    x: 7
}

var xVal = obj1.getX(); //this = obj1

var boundedFunc = obj1.getX.bind(obj2); //this = obj2
var boundedXVal = boundedFunc();

console.log(xVal); //prints 5
console.log(boundedXVal); //prints 7
Mauricio Trajano
  • 2,007
  • 1
  • 14
  • 23
0

This is mostly speculation on my part as I do not know the exact answer to your question, but I believe it is because whenever you use "this" you are inside of an object. You cannot change an object that you are already inside of because this would produce some unwanted results. Individual things inside of "this" is fine thought

this.someVariable = "something else"
WWZee
  • 457
  • 1
  • 8
  • 20