J
johnhax
Hi, all:
I'm sorry because I don't know whether here is the right place to
discuss it. We are talking about closure in
javascript, and we have some questions about ECMA-262 3rd edition:
What is joined function object(ecma-262 13.1)?
The spec have given a example:
function A() {
function B(x) { return x*x }
return B
}
var b1 = A();
var b2 = A();
Spec says: b1 and b2 can be joined, and implementation may make b1 and
b2 the same object because [[scope]] of them have no difference.
Two call of A() will produce a function from the same FunctionBody, so
they are equated and the result function objects can be joined, am I
right? What about this code:
function C(x) {
function D() { return x*x }
return D
}
var d1 = C(1);
var d2 = C(2);
Are these two call of A() also are equated uses of the same source?
And can d1 anb d2 be joined in this case even their [[scope]] is
different?
If they can be joined, and as the definition of joined object(13.1.2),
d1 === d2 should return true. That's so strange because d1 === d2, but
d1() != d2()
I've read spec several times, but still confused.
In fact, I tested many js engine and no implementation join b1 and b2
(and which will make b1 === b2 = true) as spec (except dmdscript, but
it not support closure at all!). I know joining them is optional, but
if there is any implementation which join them, then it and current
impl may get diff result from the same code. Example:
function A() {
return function () {
return arguments.callee.test;
}
}
var x = A();
var y = A();
x.test = 1;
y.test = 2;
print(x == y);
print(x());
print(y());
x and y can be joined, and their [[scope]] are equal, so impl can make
x and y the same object, so this code will print true, 2 and 2. But
current implements choose to not join them, print false, 1 and 2.
I believe optimization should never change the semantics of a program,
is joined object a mistake of the spec so that no impl support this
bug feature. Or, maybe I misunderstand the spec.
BTW, I know in spidermonkey b1.__proto__ == b2.__proto__, it some like
joined function, but they are not real joined object because b1 != b2.
I'm sorry because I don't know whether here is the right place to
discuss it. We are talking about closure in
javascript, and we have some questions about ECMA-262 3rd edition:
What is joined function object(ecma-262 13.1)?
The spec have given a example:
function A() {
function B(x) { return x*x }
return B
}
var b1 = A();
var b2 = A();
Spec says: b1 and b2 can be joined, and implementation may make b1 and
b2 the same object because [[scope]] of them have no difference.
Two call of A() will produce a function from the same FunctionBody, so
they are equated and the result function objects can be joined, am I
right? What about this code:
function C(x) {
function D() { return x*x }
return D
}
var d1 = C(1);
var d2 = C(2);
Are these two call of A() also are equated uses of the same source?
And can d1 anb d2 be joined in this case even their [[scope]] is
different?
If they can be joined, and as the definition of joined object(13.1.2),
d1 === d2 should return true. That's so strange because d1 === d2, but
d1() != d2()
I've read spec several times, but still confused.
In fact, I tested many js engine and no implementation join b1 and b2
(and which will make b1 === b2 = true) as spec (except dmdscript, but
it not support closure at all!). I know joining them is optional, but
if there is any implementation which join them, then it and current
impl may get diff result from the same code. Example:
function A() {
return function () {
return arguments.callee.test;
}
}
var x = A();
var y = A();
x.test = 1;
y.test = 2;
print(x == y);
print(x());
print(y());
x and y can be joined, and their [[scope]] are equal, so impl can make
x and y the same object, so this code will print true, 2 and 2. But
current implements choose to not join them, print false, 1 and 2.
I believe optimization should never change the semantics of a program,
is joined object a mistake of the spec so that no impl support this
bug feature. Or, maybe I misunderstand the spec.
BTW, I know in spidermonkey b1.__proto__ == b2.__proto__, it some like
joined function, but they are not real joined object because b1 != b2.