Newbie to JavaScript here.
How do I reference member foo
from within member foobar
, given that foobar
's in a closure?
var priv = {
foo: "bar",
foobar: (function() {
return this.foo === "bar";
})()
};
The code above fails. In it, this.foo
is undefined
. If I change this.foo
to priv.foo
, it's still undefined
. How do I reference priv.foo
from within the foobar
closure?
It's impossible to read any properties of an object in its defination during its initialization since prev
will be undefined at that time.
When you're trying to call a clojure inside it, it refers to undefined this
or priv
.
Probably you wanted to write:
foobar: (function() {
return this.foo === "bar";
})
without ()
in the end. And then you could call it as priv.foobar();
If you still need to call it, you could define foobar
after foo
:
var priv = {
foo: "bar"
};
priv.foobar = (function() {
return priv.foo === "bar";
})()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With