4

If I have The code:

function RandomObjectThatIsntNamedObjectWhichIOriginallyNamedObjectOnAccident() {
    this.foo = 0;
    this.bar = function () {
        this.naba = function () {
            //How do I reference foo here?
        }
    }
}
2
  • 5
    On a side note, Object is a terrible function name. You can override javascript's Object Commented Aug 29, 2011 at 19:50
  • 1
    It depends on what you are going to do with naba. How are you calling it? What are you doing with Object? What this refers to eventually depends on how you are calling the functions. As long as we don't know this, we can only guess what would be the best solution. Commented Aug 29, 2011 at 20:05

5 Answers 5

11

You need a self reference:

function RandomObjectThatIsntNamedObjectWhichIOriginallyNamedObjectOnAccident() {
    var self = this;
    this.foo = 0;
    this.bar = function () {
        this.naba = function () {
            self.foo; //foo!
        }
    }
}
Sign up to request clarification or add additional context in comments.

4 Comments

I thought about using that, but was concerned that it might actually create a copy of the object instead of just creating a reference. Thanks a lot everyone!
Of course, you could never create an "Object" since it overrides Object
@ConnerRuhl -- no problem ^_^
@IAbstractDownvoteFactory -- I know that, i was just going off the OP's post. did not really think abt the object's name.
6
function SomeObject() {
    var self = this;
    this.foo = 0;
    this.bar = function () {
        this.naba = function () {
            self.foo;
        }
    }
}

2 Comments

+1 for getting in 2 seconds after me ^_^
I blame the captchas, I would have been 20 seconds earlier ;)
3

Try the following

function SomeObject() {
    var self = this;
    this.foo = 0;
    this.bar = function () {
        this.naba = function () {
            //How do I reference foo here?
            self.foo
        }
    }
}

4 Comments

You should really use var self, because this.self will make self publicly accessible.
@Rocket, well, no, it will just have the same issue as before... where you cannot access it, i do not see how this works.
@JaredPar -- good fix, and it makes me all warm and fuzzy inside that users with >150K rep can still make mistakes :-P
@Neal I make tons of mistakes, especially if I post before I finish the morning coffee :)
1

First: Don't name your function Object, it will shadow the global Object constructor.

I don't see a reason why you have to assign naba inside bar to the instance. Why are you doing this? You can assign both bar and naba to the functions prototype:

function MyConstructor() {
    this.foo = 0;
}

MyConstructor.prototype.bar = function () {

};

MyConstructor.prototype.naba = function () {
    // this.foo
};

Eventually it depends on how you are calling the naba function. The fact that you are assigning it to this suggests you want to call it with

var obj = new MyConstructor();
obj.naba();

If you want to add naba only after bar was called, you can still access foo via this.foo:

MyConstructor.prototype.bar = function () {
    if(!this.naba) {
        this.naba = function() {
            // this.foo
        };
    }
};

var obj = new MyConstructor();
obj.bar();
obj.naba();

If you want a proper/better answer, you have to show how you are going to use naba.

4 Comments

What you're suggesting is not equivalent. naba gets assigned to bar, not the object. I general I would suggest you don't use this or prototype at all because it's very error prone. Most people seem to completely misunderstand how JavaScript works (and that it works without prototype). If you'd like I could post an equivalent solution that doesn't use this or prototype.
@Frits: I think I understand how it works. naba never gets assigned to bar, it gets assigned to whatever this refers to. Most likely it is an instance of the constructor function, but it could as well be window. But this is never bar. And if you have a constructor function, then using a prototype is the preferred way to attach instance methods (saves memory). And I don't see what is error prone with this or prototype. It might be difficult to understand at first, but there is nothing overall mystical about it.
It seems you are correct, and it's exactly what I mean, this is confusing. Here's an example: jsfiddle.net/JZ7c2
@Fritz: What this refers to depends on how the function is called. There are four different ways, see my answer here. Which means that in order to give a proper answer, we would have to know how the OP is calling all these functions.
0

Interestingly enough, you don't need to do anything special.

The this reference works:

function SomeObject() {
  this.foo = 0;
  this.bar = function () {
    this.naba = function () {
      alert(this.foo); // this works!
    }
  }
}

Since you're assigning "methods" always to the same reference, this will still point to it inside bar, and later inside naba.

14 Comments

That is when you Object is a function and NOT an object. try it with new. then it does not work.
I think this is what you meant to show: jsfiddle.net/t2Qha/1 (which suprisingly works)
@Neal: When you call var obj = new Object(); obj.bar(); obj.naba(); then it will work, because this always references obj.
@Neal: Come on, there have been like 25 seconds between our comments. Do you inspect me to refresh the page every 10 seconds? ;)
@Neal: yes, your example is better, but the code in the answer is still the same... thanks.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.