can i use y-combinator to get object reference for this closure?
this closure works:
var o = {
foo: 5
};
o.handler = function(obj){
return function() {
alert(obj.foo);
};
}(o);
o.handler(); //alert('5')
is it possible to define handler in-line, perhaps with something similar to a y-combinator operation?
var o = {
foo: 5,
handler: function(obj){
return function() {
alert(obj.foo);
};
}(o); //pointer to o? -----------------------------
};
out of aca开发者_运维技巧demic curiosity, I'm not trying to do this in production code
intro to the y-combinator
No, this is not possible, because at the time of definition of the object literal, the variable o
is undefined, and the this
reference at the time of definition doesn't reference o
.
If you used a temporary reference to this
in the outer function and passed it into the closure, it would work, but you would not be able to pass in an object to get the foo
property out of.
var o = {
foo: 5,
handler:function(){
var self = this;
return function() {
alert(self.foo);
};
}
};
var h = o.handler();
h();
This is not possible with an object literal by itself, as others have pointed out. However, the process can be wrapped. Wether or not it "adds anything" is debatable. This isn't the same as a Y-Combinator (perhaps half of it?), because it is not trying to give a "recursive name" (as far as I can tell Y-Combinators assume first-class functions and closures or a way to simulate such).
function bindMany (dict, res) {
res = res || {}
for (var k in dict) {
if (dict.hasOwnProperty(k)) {
var v = dict[p]
res[k] = v instanceof Function ? v(res) : v
}
}
return res
}
var o = bindMany({
foo: 5,
handler: function(obj){
return function() {
alert(obj.foo)
}
}
})
Not-tested, but shows an approach that could be taken. There are subtle issues with this and a prototype chain on dict
/res
, if any -- an exercise for the reader.
Happy coding.
You could just use the this
keyword here:
var o = {
foo: 5,
handler: function() {
alert(this.foo);
}
};
That's a lot easier approach... Actually, your approach isn't even possible, as o
is not defined when you refer to it.
精彩评论