i'm not trolling. unlike javascript, most other languages deal near exclusively
with blocking-code, which is only applicable to low-level library-code in
javascript (that pretty much anyone can write). at higher-level
integration-stuff dealing with non-blocking io, these blocking-code
design-patterns from other languages are pretty-much useless. like say …
*block-level scoping*, because it doesn’t mix well with closures, which are
heavily used in integration-level javascript to keep track of states for
non-blocking code.
e.g.
```
/*jslint
bitwise: true,
browser: true,
maxerr: 8,
maxlen: 200,
node: true,
nomen: true,
regexp: true,
stupid: true
*/
(function () {
'use strict';
var chunkList, request, response, timerTimeout;
chunkList = [];
// auto-timeout and cleanup closure-vars request and response after 30000 ms
timerTimeout = setTimeout(function () {
try {
request.destroy();
} catch (ignore) {
}
try {
response.destroy();
} catch (ignore) {
}
console.error('timeout error after 30000 ms');
}, 30000);
request = require('https').request(
require('url').parse('https://www.example.com'),
function (_response) {
response = _response;
response.on('data', function (chunk) {
// append chunk to closure-var chunkList
chunkList.push(chunk);
});
response.on('end', function () {
// print closure-var chunkList to stdout
console.log(Buffer.concat(chunkList).toString());
// cancel closure-var timerTimeout,
// after request successfully completes
clearTimeout(timerTimeout);
});
}
);
request.end();
/*
output:
<!doctype html>
<html>
<head>
<title>Example Domain</title>
...
</head>
<body>
<div>
<h1>Example Domain</h1>
<p>This domain is established to be used for illustrative examples in
documents. You may use this
domain in examples without prior coordination or asking for permission.</p>
<p><a href="http://www.iana.org/domains/example">More information...</a></p>
</div>
</body>
</html>
*/
}());
```
> On Mar 22, 2018, at 2:31 AM, Christopher Thorn <[email protected]> wrote:
>
> This is just trolling that seems intended to derail the discussion.
>
> On Wed, Mar 21, 2018 at 10:21 AM, kai zhu <[email protected]
> <mailto:[email protected]>> wrote:
> how is any of this less-confusing than a simple style-guide of pre-declaring
> all variables @ the beginning of a function?
> again, there’s zero legitimate reason why javascript even needs block-level
> scoping of variables (and arguing because other languages have it is not a
> reason). you're just creating more needless javascript style-guide variance
> that further confuses your team-members / project-successor.
>
> ```
> /*jslint
> bitwise: true,
> browser: true,
> maxerr: 8,
> maxlen: 100,
> node: true,
> nomen: true,
> regexp: true,
> stupid: true
> */
> (function () {
> 'use strict';
> // es5 best-practice of declaring all variables at beginning of function,
> // and runtime-checked by 'use strict' pragma
> var bar,
> c0,
> c1,
> foo,
> getTeamArray,
> people,
> v1,
> xx,
> yy;
>
>
>
> getTeamArray = function () {
> return [1, 2, 3];
> };
> people = getTeamArray();
> if (people.length > 2) {
> console.log("it's a crowd", people.join(','));
> } else if (people.length === 2) {
> console.log("just a pair");
> } else if (people.length === 1) {
> console.log("solo");
> } else {
> console.log("none");
> }
> // output: it's a crowd 1,2,3
>
>
>
> bar = function () {
> return 0;
> };
> foo = function () {
> return 50;
> };
> (function () {
> xx = foo();
> if (xx < 50) {
> console.log(xx); // yy is not available here
> return;
> }
> yy = bar();
> if (yy > 0) {
> console.log(yy);
> return;
> }
> // else
> console.log(xx, yy);
> }());
> // output: 50 0
>
>
>
> c0 = null;
> c1 = function (xx) {
> return xx;
> };
> v1 = false;
> // 'unset' xx
> xx = undefined;
> (function () {
> if (c0) {
> console.log(c0); // xx is not available here
> return;
> }
> xx = v1;
> if (c1(xx)) {
> console.log(xx);
> return;
> }
> // else
> console.log(c0, xx);
> }());
> // output: null false
> }());
> ```
>
>> On Mar 22, 2018, at 1:56 AM, Mike Samuel <[email protected]
>> <mailto:[email protected]>> wrote:
>>
>>
>>
>> On Wed, Mar 21, 2018 at 11:02 AM, Rodrigo <[email protected]
>> <mailto:[email protected]>> wrote:
>> My proposal is to keep it simple and implement the if-assignment `if(
>> const o = ...; ! o ) { ... }` mimicking the `for(;;)` assignment
>> behavior.
>>
>> Fair enough. If the assignment is separate from the condition, then none of
>> that matters.
>> That question only pertained to let/const declarations used for their value.
>>
>> That way we can scope a variable to the `if` block and we can do that
>> *separately* from assignment.
>>
>> Assigning and comparing at the same time opens up all sort of oddities
>> as the variable transitions from lvalue to comparison operand. This
>> already exists in the language and is definitely not good practice.
>>
>> `if.value`, as proposed earlier is a bombshell similar to `this`,
>> which could easily get scrambled with the introduction of additional
>> `if` blocks.
>>
>> The beauty of `if(;)` is that it makes scope blocks possible while
>> avoiding the asymmetric behavior caused by assigning and comparing in
>> one go.
>>
>> if( let people=getTeamArray(); people.length > 2 ) {
>> console.log("it's a crowd", people.join(','));
>> }
>> else if( people.length == 2 ) {
>> console.log("just a pair");
>> }
>> else if( people.length == 1 {
>> console.log("solo");
>> }
>> else {
>> console.log("none");
>> }
>>
>> IIUC, it sounds like the right side could be syntactic sugar for the left
>> side.
>> If that's right, what about the left side warrants new syntax to enable the
>> right?
>>
>>
>> Current Proposed
>> {
>> let people = getTeamArray();
>> if( people.length > 2 ) {
>> console.log("it's a crowd", people.join(','));
>> }
>> else if( people.length == 2 ) {
>> console.log("just a pair");
>> }
>> else if( people.length == 1 {
>> console.log("solo");
>> }
>> else {
>> console.log("none");
>> }
>> }
>> if( let people = getTeamArray(); people.length > 2 ) {
>> console.log("it's a crowd", people.join(','));
>> }
>> else if( people.length == 2 ) {
>> console.log("just a pair");
>> }
>> else if( people.length == 1 {
>> console.log("solo");
>> }
>> else {
>> console.log("none");
>> }
>>
>> I can imagine that it might be nice when used later in an else-if chain:
>>
>> if (c0) {
>> ...
>> } else (let x = v1, c1(x)) {
>> ...
>> } else ...
>>
>> vs
>>
>> if (c0) {
>> ...
>> } else {
>> let x = v1;
>> if (c1(x)) {
>> ...
>> } else ...
>> }
>>
>> Can you point at any particularly tricky code that could be simplified by
>> this?
>> _______________________________________________
>> es-discuss mailing list
>> [email protected] <mailto:[email protected]>
>> https://mail.mozilla.org/listinfo/es-discuss
>> <https://mail.mozilla.org/listinfo/es-discuss>
>
> _______________________________________________
> es-discuss mailing list
> [email protected] <mailto:[email protected]>
> https://mail.mozilla.org/listinfo/es-discuss
> <https://mail.mozilla.org/listinfo/es-discuss>
>
>
_______________________________________________
es-discuss mailing list
[email protected]
https://mail.mozilla.org/listinfo/es-discuss