Difference between self and this in javascript and when to use either of them [duplicate]
Asked Answered
A

2

11

Possible Duplicate:
What underlies this JavaScript idiom: var self = this?

I am confused that when to use self and this in javascript.
I know that this refers to current context and self refers to current window.
As I am developing an application in Titanium. I want to know when to use self or this OR does is there is any concept of self in titanium development.

here is the sample code that i am doing in my titanium commonJS module

var auth = require('/SDKTest/auth');
var nodeAPI = require('/SDKTest/nodeAPI');
function myAPI() {
     this.auth = auth;
     this.nodeAPI = nodeAPI;
     return this;
    }
module.exports = myAPI;

This code works, but can I use self in place of this ? And instead of using this i can create a namespace and do something like this:

  function myAPI() {
     var api = {};
     api.auth = auth;
     api.nodeAPI = nodeAPI;
     return api;
    }

both the approaches work but what is the use of using this over here

Astolat answered 4/10, 2012 at 13:19 Comment(2)
Refer https://mcmap.net/q/21450/-var-self-thisCarouse
i have updated my question added a sample code over thereAstolat
R
3

Your question is somewhat confusing, it's like asking: Do I buy apples or tomatoes? The answer is, it really depends on what you want to do, since they're completely different.

Essentially, you have answered your own question to some extent, because you already know the differences between the two:

  • this refers to the current context
  • self refers to window
function myAPI() {
     this.auth = auth;
     this.nodeAPI = nodeAPI;
     return this;
    }
module.exports = myAPI;

You're asking whether you can use self instead. Think about it, what does this allow you to do? It allows you, to refer to the context. What is the context, well, it's module when you call module.exports(). And module is most likely not going to be window, so no, you can't use self here.

Does that answer the question?

The second code example seems to do something completely different. I don't know what to make of that.

Rochus answered 4/10, 2012 at 14:12 Comment(2)
@phantOm In second code i am just creating my own custom namespace which holds the auth and nodeAPI objectsAstolat
@AjeetPratapMaurya yes, I see that. The thing is, that it's completely different. If you were to assign that one to module.exports, it would return module.exports() a new object with attributes auth and nodeAPI, rather than modifying module itself, as the first code does.Rochus
D
9

self is not a JavaScript keyword! Programmers use that when defining classes to have always valid reference to object itself.

var Person = function() {
    var self = this;
    // private function
    function say(what) {
        alert(what);
    }
    self.fetchSomething = function() {
        var xhr = Ti.Network.createHTTPClient({
            onload: function() {
                // in this case 'this' is referencing to xhr!!!
                say(this.responseText);
            }
        });
        xhr.open('GET', 'http://www.whatever.com');
        xhr.send();
    }
    return self;
}
var p = new Person();
p.fetchSomething();
Derward answered 5/10, 2012 at 10:46 Comment(0)
R
3

Your question is somewhat confusing, it's like asking: Do I buy apples or tomatoes? The answer is, it really depends on what you want to do, since they're completely different.

Essentially, you have answered your own question to some extent, because you already know the differences between the two:

  • this refers to the current context
  • self refers to window
function myAPI() {
     this.auth = auth;
     this.nodeAPI = nodeAPI;
     return this;
    }
module.exports = myAPI;

You're asking whether you can use self instead. Think about it, what does this allow you to do? It allows you, to refer to the context. What is the context, well, it's module when you call module.exports(). And module is most likely not going to be window, so no, you can't use self here.

Does that answer the question?

The second code example seems to do something completely different. I don't know what to make of that.

Rochus answered 4/10, 2012 at 14:12 Comment(2)
@phantOm In second code i am just creating my own custom namespace which holds the auth and nodeAPI objectsAstolat
@AjeetPratapMaurya yes, I see that. The thing is, that it's completely different. If you were to assign that one to module.exports, it would return module.exports() a new object with attributes auth and nodeAPI, rather than modifying module itself, as the first code does.Rochus

© 2022 - 2024 — McMap. All rights reserved.