js: allow functions in blocks (ES6)

Talk about code development, features, specific bugs, enhancements, patches, and similar things.
Forum rules
Please keep everything here strictly on-topic.
This board is meant for Pale Moon source code development related subjects only like code snippets, patches, specific bugs, git, the repositories, etc.

This is not for tech support! Please do not post tech support questions in the "Development" board!
Please make sure not to use this board for support questions. Please post issues with specific websites, extensions, etc. in the relevant boards for those topics.

Please keep things on-topic as this forum will be used for reference for Pale Moon development. Expect topics that aren't relevant as such to be moved or deleted.
pmuser2
Hobby Astronomer
Hobby Astronomer
Posts: 26
Joined: 2018-02-09, 04:35

js: allow functions in blocks (ES6)

Unread post by pmuser2 » 2018-06-11, 20:13

Pale Moon 27.9.2, unlike at least Chrome, Edge and IE, still does not support functions inside blocks (for example, "if") in scripts:
"SyntaxError: in strict mode code, functions may be declared only at top level or immediately within another function"
I suggest to fix it.
Last edited by pmuser2 on 2018-06-11, 20:54, edited 2 times in total.

vannilla
Moon Magic practitioner
Moon Magic practitioner
Posts: 2183
Joined: 2018-05-05, 13:29

Re: js: allow functions in blocks (ES6)

Unread post by vannilla » 2018-06-12, 00:57

As per https://developer.mozilla.org/en-US/doc ... trict_mode , functions can't be defined inside blocks when using strict mode.
Pale Moon is behaving as expected.
If you want block-level functions, make sure your script is not in strict mode.

User avatar
Moonchild
Pale Moon guru
Pale Moon guru
Posts: 35474
Joined: 2011-08-28, 17:27
Location: Motala, SE
Contact:

Re: js: allow functions in blocks (ES6)

Unread post by Moonchild » 2018-06-12, 07:19

Defining functions inside conditionals is really bad practice, you should never do this.
Instead, consider assigning functions to variables:

Code: Select all

var a = function() { /code/ };
In sloppy mode in browsers, function statements are permitted "everywhere". This is not part of ES5 (or even ES3)! It's an extension with incompatible semantics in different browsers, meaning your results may differ depending on which browser you're using. You don't want that :)
"Sometimes, the best way to get what you want is to be a good person." -- Louis Rossmann
"Seek wisdom, not knowledge. Knowledge is of the past; wisdom is of the future." -- Native American proverb
"Linux makes everything difficult." -- Lyceus Anubite

pmuser2
Hobby Astronomer
Hobby Astronomer
Posts: 26
Joined: 2018-02-09, 04:35

Re: js: allow functions in blocks (ES6)

Unread post by pmuser2 » 2018-06-12, 11:19

I can admit that it's bad practice. It's semantically strange.
On the other hand, JavaScript as a whole is strange. Function-inside-if seems logical to me when it's needed only on some condition.

But, from the specification point of view, it's there since 2015, year and language, ECMA-262 6th Edition.
Until ES 6, block could really contain only statements. Beginning with ES 6, block can contain both statements and declarations:
http://ecma-international.org/ecma-262/ ... #sec-block
http://ecma-international.org/ecma-262/ ... #sec-block
http://ecma-international.org/ecma-262/ ... #sec-block

I'm against programs' overcomplication. Let's just be honest and state, e.g., that Pale Moon will unlikely support that particular feature. But one shouldn't deny it's normative existence.

User avatar
Moonchild
Pale Moon guru
Pale Moon guru
Posts: 35474
Joined: 2011-08-28, 17:27
Location: Motala, SE
Contact:

Re: js: allow functions in blocks (ES6)

Unread post by Moonchild » 2018-06-14, 09:29

I'm not stating anything of that sort because it's incorrect and I won't be goaded into making statements that can be abused by opponents of our effort to repeat a few more times that "we're an obsolete firefox" which is incorrect on so many levels.

FYI: Function definitions can be placed in blocks in Pale Moon in non-strict mode (but with the limitation that they need to be defined before they are used!).

Instead I will state:
ES6 support to almost its full potential will be present in Pale Moon 28, including block-level function support where declarations don't have to be at the top of the block.
You can verify this by looking up the kangax ECMAScript support table from our unstable version of the browser.
"Sometimes, the best way to get what you want is to be a good person." -- Louis Rossmann
"Seek wisdom, not knowledge. Knowledge is of the past; wisdom is of the future." -- Native American proverb
"Linux makes everything difficult." -- Lyceus Anubite

pmuser2
Hobby Astronomer
Hobby Astronomer
Posts: 26
Joined: 2018-02-09, 04:35

Re: js: allow functions in blocks (ES6)

Unread post by pmuser2 » 2018-06-14, 11:59

Yes, it works in unstable.
As for mode, strict is "true", non-strict is not.
Thank you for the answer. I should have checked unstable version before posting.

Locked