Path: csiph.com!news.redatomik.org!weretis.net!feeder4.news.weretis.net!feeder5.news.weretis.net!news.solani.org!.POSTED!not-for-mail From: Thomas 'PointedEars' Lahn Newsgroups: comp.lang.javascript Subject: Re: Understanding someone's code Date: Thu, 30 Mar 2017 18:13:07 +0200 Organization: PointedEars Software (PES) Lines: 123 Message-ID: <2373677.mvXUDI8C0e@PointedEars.de> References: <34qdnaF51IrJJUHFnZ2dnUU7-WednZ2d@westnet.com.au> Reply-To: Thomas 'PointedEars' Lahn Mime-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 8Bit X-Trace: solani.org 1490890388 30187 eJwNysERACAIA7CVUGjRcUBk/xH0mbtAOXjcCBoaXZULihtb2m/XyptzOr6MQUopYsmQs3n+fi23EZc= (30 Mar 2017 16:13:08 GMT) X-Complaints-To: abuse@news.solani.org NNTP-Posting-Date: Thu, 30 Mar 2017 16:13:08 +0000 (UTC) User-Agent: KNode/4.14.2 Face: iVBORw0KGgoAAAANSUhEUgAAADAAAAAwBAMAAAClLOS0AAAAGFBMVEXTxa4RFk5dUWANED8PFEfy7+MGBiW+n3ZNF/QuAAACaElEQVQ4jVXUwVOcMBQG8Dc7Rc4PUntdWV2uxjDpGaGeozOp1woar4jd5t/v9wLstMwsA/ntlxdCAgUc1hjTc9/JCZfGoo3wG3HdmdAWrIJRHe7GM/TmpY5VFefuVcAkkPbLIaN8rmPmjloyZxgyR3GuJ4K0AGtJ2htz8o7yqikm759fldQXaMpbDzjKAG+8v+AugVTOPO5DOjLvGtUYQwh0CPjnVMyGd+8/GfUB5nLKJDD2aLDh5HYyMDJGDwQIo2ZmZcKbowNmAdB/AzyFhrmF2MHRb0QJJfaAnwGB6orZhoykLzJtGwF/xpYxI1dswomiUj3gTuAIqCn/4C7cULwGNBtwMTk3Y4LfKB5YUaOKBKYtpplm7u0vip8tU1NWWyI/7XdcSuIDoMt6rVHMWT0DbjHPGqDqZVSa6zleLcUTcIKLoMv3ueJluALtAo9B302zPPlrtiVScRdCjXvVh3e3JpYa/jjkuC9N+LrBMlz/eAN4eQijX2EdLo6c5tGGHwLyHFtXk89dDGHwCVhG9T0S/j55AhRZgkMCmUQXJ49TnS1wnQDvw0eAh9ICeMmEFbCnPMFzjAvsWoEWEFdYEx+S0MoUZ1gT1wId8+AF3Bl2OoEu906AUHx5VLw/gXYg/x84loOah/2UYNrgiwSwGO7RfUzVBbx/kgpckumGOi6QirtD6gkLTitbnxNol47S2jVc2vsN5kPqaAHT8uUdAJM4v/DanjYOwmUjWznGfwB7sGtAtor5BgofDuzaRj4kSQAqDakTsKORa3Q3xKi3gE1fhl71KRMqrdZ2AWNNg/YOhQyrVBnb+i+nEg4bsDA+egAAAABJRU5ErkJggg== X-User-ID: eJwNy8EBwCAIA8CVUEjAcUBh/xHa+x+Ui9eNoGEwDpFVrSeG4rczdd8yLHRngNGTQuZ5uuO+bRDwD1Nd/gIfUAQVwg== Cancel-Lock: sha1:WdvhcbzoAJHPQ8vPg00soL72ck0= X-Face: %i>XG-yXR'\"2P/C_aO%~;2o~?g0pPKmbOw^=NT`tprDEf++D.m7"}HW6.#=U:?2GGctkL,f89@H46O$ASoW&?s}.k+&. On 30/03/2017 3:26 PM, Andrew Poulos wrote: >> I'm looking through some code I found online and one bit has this >> >> function k() { >> var a; >> if (b.onScroll) b.onScroll(); >> b.getPageInView && (a = b.getPageInView()) && (F = a, >> document.getElementById("pageNumber").value = a); >> } >> >> which has me confused. Is the line >> >> b.getPageInView && (a = b.getPageInView()) && (F = a, >> document.getElementById("pageNumber").value = a); >> >> equivalent to >> >> if (b.getPageInView) { >> a = b.getPageInView(); >> F = a; >> document.getElementById("pageNumber").value = a; >> } > > Or should I have said, is equivalent to > > if (b.getPageInView) { > a = b.getPageInView(); > if (a) { > F = a; > document.getElementById("pageNumber").value = a; > } > } 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: | | Twitter: @PointedEars2 | Please do not cc me./Bitte keine Kopien per E-Mail.