If “F” is a variable, too, then yes.
In general, no. It is a seldom-known fact that the result of an assignment
operation in ECMAScript is the value of the *right-hand side* *before*
assignment; it is _not_ the value of the left-hand side after assignment.
Therefore, the second code is only equivalent to the first one if the
assignment is successful and there is no setter to modify the assigned value
as then the left-hand side and the right-hand side of the assignment are
equal.
Counter-example:
/*
* “foo” is declared as a variable that holds a reference to an object
* with empty prototype chain (which I call a “data object”; see
* jsx.object.createDataObject(), formerly jsx.object.getDataObject()).
* The properties of that object, and their attributes, are defined
* in the following.
*/
var foo = Object.create(null, (function () {
var _bar;
return {
/*
* “bar” is defined as a property that has a setter and a getter;
* Whatever value “bar” is set to is ignored, and it is set to 23
* instead. The same could be achieved with an empty setter and
* a getter that always returns 23.
*/
"bar": {
"set": function () {
_bar = 23;
},
"get": function () {
return _bar;
}
}
}
}()));
/* 42 */
console.log(foo.bar = 42);
/* 23 */
console.log(foo.bar);
There is no way to write code strictly equivalent to the first one without
using additional variables/properties (to hold the original return value)
because repeated method calls and variable/property accesses need not return
the same value as before (although in this case it is safe to assume that
the value of the local variable “a” cannot be changed after it has been
assigned to). IOW, the code
function k ()
{
var a;
if (b.onScroll) b.onScroll();
if (b.getPageInView)
{
var a = b.getPageInView();
if (b.getPageInView())
{
F = a;
if (a)
{
document.getElementById("pageNumber").value = a;
}
}
}
}
is equivalent, but not strictly equivalent, because b.getPageInView() can
return a different value the second time.
It should also be noted that the conversion of “b.getPageInView” to a true-
value, if possible, does not necessarily mean that “b.getPageInView” can be
called. As I have explained often before, this test is, in general, both
insufficient and error-prone. See also jsx.object.isMethod ===
jsx.object.isHostMethod, and jsx.object.isNativeMethod() (the latter is
deprecated for trivial cases in favor of a simple “typeof x == "function"”).
--
PointedEars
FAQ: <
http://PointedEars.de/faq> | <
http://PointedEars.de/es-matrix>
<
https://github.com/PointedEars> | <
http://PointedEars.de/wsvn/>
Twitter: @PointedEars2 | Please do not cc me./Bitte keine Kopien per E-Mail.