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


Groups > comp.lang.javascript > #11871 > unrolled thread

Mystical incantation of the day

Started byRobG <rgqld@iinet.net.au>
First post2012-02-18 12:40 +1000
Last post2012-02-18 15:36 +0000
Articles 9 — 5 participants

Back to article view | Back to comp.lang.javascript


Contents

  Mystical incantation of the day RobG <rgqld@iinet.net.au> - 2012-02-18 12:40 +1000
    Re: Mystical incantation of the day "Richard Cornford" <Richard@litotes.demon.co.uk> - 2012-02-18 06:17 +0000
      Re: Mystical incantation of the day Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-02-18 09:56 +0100
        Re: Mystical incantation of the day "Richard Cornford" <Richard@litotes.demon.co.uk> - 2012-02-18 15:36 +0000
    Re: Mystical incantation of the day Asen Bozhilov <asen.bozhilov@gmail.com> - 2012-02-18 01:30 -0800
      Re: Mystical incantation of the day "Richard Cornford" <Richard@litotes.demon.co.uk> - 2012-02-18 15:37 +0000
    Re: Mystical incantation of the day Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-02-18 11:32 +0100
    Re: Mystical incantation of the day insin <jonathan.buchanan@gmail.com> - 2012-02-18 04:21 -0800
      Re: Mystical incantation of the day "Richard Cornford" <Richard@litotes.demon.co.uk> - 2012-02-18 15:36 +0000

#11871 — Mystical incantation of the day

FromRobG <rgqld@iinet.net.au>
Date2012-02-18 12:40 +1000
SubjectMystical incantation of the day
Message-ID<R4KdnYJKPoE1kqLSnZ2dnUVZ_u2dnZ2d@westnet.com.au>
I seem to remember a thread about these things some time ago, anyway 
came across a new one:

   (function(global) {
     ...
   })(typeof window === 'undefined' ? this : window);

Lots to ponder there.


-- 
Rob

[toc] | [next] | [standalone]


#11875

From"Richard Cornford" <Richard@litotes.demon.co.uk>
Date2012-02-18 06:17 +0000
Message-ID<E4-dnVkFt-Pw36LSnZ2dnUVZ8jadnZ2d@giganews.com>
In reply to#11871
RobG wrote:
> I seem to remember a thread about these things some time ago,
> anyway came across a new one:
>
>   (function(global) {
>     ...
>   })(typeof window === 'undefined' ? this : window);
>
> Lots to ponder there.

I was thinking back to that thread earlier in week when I encountered 
this code in an object literal:-

oPlugin = {
  ...
  Browser: {
    Explorer:!!(navigator.appName=='Microsoft Internet Explorer'),
    Other:  !!!(navigator.appName=='Microsoft Internet Explorer'),
    Chrome:  !!(navigator.userAgent.toLowerCase().indexOf('chrome')>-1)
  },
  ...
}

It is that middle - Other - property definition that really demonstrates 
someone doing something with a serious misconception of why they are 
doing it.

Richard. 

[toc] | [prev] | [next] | [standalone]


#11885

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2012-02-18 09:56 +0100
Message-ID<1937453.aqSPkdTlGX@PointedEars.de>
In reply to#11875
Richard Cornford wrote:

> oPlugin = {
>   ...
>   Browser: {
>     Explorer:!!(navigator.appName=='Microsoft Internet Explorer'),
>     Other:  !!!(navigator.appName=='Microsoft Internet Explorer'),
>     Chrome:  !!(navigator.userAgent.toLowerCase().indexOf('chrome')>-1)
>   },
>   ...
> }
> 
> It is that middle - Other - property definition that really demonstrates
> someone doing something with a serious misconception of why they are
> doing it.

It is the needless double negation of an intrinsically boolean value in the 
`Explorer:' line that shows me the cluelessness of the author already.


PointedEars
-- 
Anyone who slaps a 'this page is best viewed with Browser X' label on
a Web page appears to be yearning for the bad old days, before the Web,
when you had very little chance of reading a document written on another
computer, another word processor, or another network. -- Tim Berners-Lee

[toc] | [prev] | [next] | [standalone]


#11897

From"Richard Cornford" <Richard@litotes.demon.co.uk>
Date2012-02-18 15:36 +0000
Message-ID<OaKdnboWjYIGWKLSnZ2dnUVZ8uKdnZ2d@giganews.com>
In reply to#11885
Thomas 'PointedEars' Lahn wrote:
> Richard Cornford wrote:
>
>> oPlugin = {
>>  ...
>>  Browser: {
>>   Explorer:!!(navigator.appName=='Microsoft Internet Explorer'),
>>   Other:  !!!(navigator.appName=='Microsoft Internet Explorer'),
>>   Chrome:  !!(navigator.userAgent.toLowerCase().indexOf('chrome')>-1)
>>  },
>>  ...
>> }
>>
>> It is that middle - Other - property definition that really
>> demonstrates someone doing something with a serious misconception
>> of why they are doing it.
>
> It is the needless double negation of an intrinsically boolean value
> in the  `Explorer:' line that shows me the cluelessness of the author
> already.

Yes, but that is only a demonstration of someone not understanding when, 
where and why it is appropriate to force type-type conversion. The next 
line adds not understanding how this type-conversion works, or at least 
not stopping to think about it.

Richard. 

[toc] | [prev] | [next] | [standalone]


#11886

FromAsen Bozhilov <asen.bozhilov@gmail.com>
Date2012-02-18 01:30 -0800
Message-ID<d32122a4-7740-4305-bfdd-6e116f4c4fed@m5g2000yqk.googlegroups.com>
In reply to#11871
RobG wrote:
> I seem to remember a thread about these things some time ago, anyway
> came across a new one:
>
>    (function(global) {
>      ...
>    })(typeof window === 'undefined' ? this : window);
>
> Lots to ponder there.

Obviously this is coded by script kid. In global execution context the
`this` value always refers to a global object. Using of `window` to
get global object here is useless and leads to confusion. If I have to
read that code, when I see formal parameter with name `global' I would
expect its value to be the value of `this` passed during invocation of
that function.

The whole idea for passing arguments to immediately executed function
expression leads readability problems. As a reader to see the actual
value of `global' I have to go to the end of the function. Usually
such functions are used for modules and therefore their body is longer
than my visible part of the monitor. I would have to scroll first to
the bottom and back again to the top.

The whole idea for that design comes from ECMAScript 5 strict-mode and
some premature lookup optimizations. In ECMAScript 5 if the function
code is strict code the `this` value is not coerced to global object,
on entering in the execution context of the function.

So this design is "defensive" and "performance" tuning. The programmer
expects some kid to put "use strict"; on the top of his code and that
would break the program. How this defense is reasonable is really
questionable. In the other hand during the debug stage strict-mode
would be helpful. Instead of passing arguments he can apply different
strategy. Using `call' method of fucntion he can explicitly provide
the value of `this` and therefore its value could be same in string
and non-strict mode.

(function () {
    //...
}).call(this);

The `this` value is always bound to the `this` of surrounded execution
context. If the code is placed in global execution context the `this`
value in that function will always be a reference to global object.

[toc] | [prev] | [next] | [standalone]


#11899

From"Richard Cornford" <Richard@litotes.demon.co.uk>
Date2012-02-18 15:37 +0000
Message-ID<zvOdnb10DvIAWKLSnZ2dnUVZ8kKdnZ2d@giganews.com>
In reply to#11886
Asen Bozhilov wrote:
> RobG wrote:
>> I seem to remember a thread about these things some time
>> ago, anyway came across a new one:
>>
>> (function(global) {
>> ...
>> })(typeof window === 'undefined' ? this : window);
>>
>> Lots to ponder there.
>
> Obviously this is coded by script kid. In global execution
> context the `this` value always refers to a global object.
> Using of `window` to get global object here is useless and
> leads to confusion. If I have to read that code, when I see
> formal parameter with name `global' I would expect its value
> to be the value of `this` passed during invocation of that
> function.
>
> The whole idea for passing arguments to immediately executed
> function expression leads readability problems. As a reader
> to see the actual value of `global' I have to go to the end
> of the function. Usually such functions are used for modules
> and therefore their body is longer than my visible part of
> the monitor. I would have to scroll first to the bottom and
> back again to the top.
>
> The whole idea for that design comes from ECMAScript 5
> strict-mode and some premature lookup optimizations. In
> ECMAScript 5 if the function code is strict code the `this`
> value is not coerced to global object, on entering in the
> execution context of the function.
>
> So this design is "defensive" and "performance" tuning. The
> programmer expects some kid to put "use strict"; on the top
> of his code and that would break the program. How this defense
> is reasonable is really questionable. In the other hand during
> the debug stage strict-mode would be helpful. Instead of passing
> arguments he can apply different strategy. Using `call' method
> of fucntion he can explicitly provide the value of `this` and
> therefore its value could be same in string and non-strict mode.
>
> (function () {
    > //...
> }).call(this);
>
> The `this` value is always bound to the `this` of surrounded
> execution context. If the code is placed in global execution
> context the `this` value in that function will always be a
> reference to global object.

That would be an explanation of why there is a - global - formal 
parameter for the function along with an object argument to the function 
call (though, as you say, not necessarily a good (i.e. well justified) 
explanation), but it does not justify the attempt to draw the 
distinction between the ECMAScript global object and the/a - window - 
object when deciding what that argument will be.

Richard. 

[toc] | [prev] | [next] | [standalone]


#11887

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2012-02-18 11:32 +0100
Message-ID<1493830.6P2SmCNXNH@PointedEars.de>
In reply to#11871
RobG wrote:

> I seem to remember a thread about these things some time ago, anyway
> came across a new one:
> 
>    (function(global) {
>      ...
>    })(typeof window === 'undefined' ? this : window);
> 
> Lots to ponder there.

For a more streamlined discussion, what exactly are the issues that you see 
with that code?


PointedEars
-- 
Anyone who slaps a 'this page is best viewed with Browser X' label on
a Web page appears to be yearning for the bad old days, before the Web,
when you had very little chance of reading a document written on another
computer, another word processor, or another network. -- Tim Berners-Lee

[toc] | [prev] | [next] | [standalone]


#11889

Frominsin <jonathan.buchanan@gmail.com>
Date2012-02-18 04:21 -0800
Message-ID<37f00bef-a0e6-4690-9806-2496081e6135@w1g2000vbg.googlegroups.com>
In reply to#11871
On Feb 18, 2:40 am, RobG <rg...@iinet.net.au> wrote:
> I seem to remember a thread about these things some time ago, anyway
> came across a new one:
>
>    (function(global) {
>      ...
>    })(typeof window === 'undefined' ? this : window);
>
> Lots to ponder there.
>
> --
> Rob

Depends how global is being used in the function body.

You sometimes see this in code which is intended to be shared between
Node.js (or other server-side JS implementations) and browsers, in
which module contents are exported to global instead of whatever
server-side module format is available.

--
Jonny.

[toc] | [prev] | [next] | [standalone]


#11898

From"Richard Cornford" <Richard@litotes.demon.co.uk>
Date2012-02-18 15:36 +0000
Message-ID<6IKdnVwrRpoGWKLSnZ2dnUVZ8tqdnZ2d@giganews.com>
In reply to#11889
insin wrote:
> On Feb 18, 2:40 am, RobG  wrote:
>> I seem to remember a thread about these things some time
>> ago, anyway came across a new one:
>>
>> (function(global) {
>> ...
>> })(typeof window === 'undefined' ? this : window);
>>
>> Lots to ponder there.
>
> Depends how global is being used in the function body.
>
> You sometimes see this in code which is intended to be shared
> between Node.js (or other server-side JS implementations) and
> browsers, in which module contents are exported to global
> instead of whatever server-side module format is available.

That may be the rationale for the code but in order for it to be valid 
it would be necessary to show circumstances where just passing in the - 
this - value as the argument and forgetting about the - window - 
reference would make a difference. Because even though Node.js may be 
executing in an environment without a - window - reference defined no 
ECMAScript can be executing in an environment where the - this - 
keyword's value is not fully specified. That is, if it is valid to use 
the - this - value anywhere it must be valid for the same code to use it 
everywhere.

Richard. 

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.javascript


csiph-web