Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #32879

Re: Understanding someone's code

From Thomas 'PointedEars' Lahn <PointedEars@web.de>
Newsgroups comp.lang.javascript
Subject Re: Understanding someone's code
Date 2017-03-30 18:13 +0200
Organization PointedEars Software (PES)
Message-ID <2373677.mvXUDI8C0e@PointedEars.de> (permalink)
References <WumdnYlb48xrFUHFnZ2dnUU7-K_NnZ2d@westnet.com.au> <34qdnaF51IrJJUHFnZ2dnUU7-WednZ2d@westnet.com.au>

Show all headers | View raw


Andrew Poulos wrote:

> 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: <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.

Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Understanding someone's code Andrew Poulos <ap_prog@hotmail.com> - 2017-03-30 15:26 +1100
  Re: Understanding someone's code Andrew Poulos <ap_prog@hotmail.com> - 2017-03-30 18:48 +1100
    Re: Understanding someone's code "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2017-03-30 12:07 +0200
      Re: Understanding someone's code tony@mountifield.org (Tony Mountifield) - 2017-03-30 11:43 +0000
        Re: Understanding someone's code "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2017-03-30 14:00 +0200
          Re: Understanding someone's code tony@mountifield.org (Tony Mountifield) - 2017-03-30 13:44 +0000
            Re: Understanding someone's code "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2017-03-30 19:07 +0200
    Re: Understanding someone's code JJ <jj4public@vfemail.net> - 2017-03-30 18:58 +0700
      Re: Understanding someone's code Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2017-03-30 18:13 +0200
    Re: Understanding someone's code Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2017-03-30 18:13 +0200
      Re: Understanding someone's code John G Harris <niam@jghnorth.org.uk.invalid> - 2017-03-31 10:30 +0100
        Re: Understanding someone's code Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2017-04-02 14:41 +0200
          Re: Understanding someone's code John G Harris <niam@jghnorth.org.uk.invalid> - 2017-04-02 19:05 +0100
            Re: Understanding someone's code Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2017-04-10 05:00 +0200
              Re: Understanding someone's code John G Harris <niam@jghnorth.org.uk.invalid> - 2017-04-10 10:03 +0100

csiph-web