Yank Named and Assigned Functions

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Yank Named and Assigned Functions

Sebastian Malton
So function definitions are currently yanked to the top. This includes named functions.

However if a function is set to a variable, even if it is named, it is not yanked. Nor is the name exposed. My suggestion is that it is.

Namely, currently the following errors:
```js
module.exports.fnName2 = function fnName (){...}

fnName()
```

But I propose that it would not. Currently this can be gotten around by making it two lines, one to define the function and another to export it but that seems clunky 

Sebastian Malton


_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Yank Named and Assigned Functions

Ben Newman
You seem to be assuming CommonJS semantics (require, exports, module), which was never part of the ECMAScript specification.

Instead, ECMAScript has exactly the syntax you're looking for:

```js
export function fnName() {...}
fnName();
```

P.S. We say "hoisted" rather than "yanked" these days. In fact, I've never heard of yanking before.

Ben

His errors are volitional and are the portals of discovery.
-- James Joyce

On Wed, Mar 21, 2018 at 5:54 PM, Sebastian Malton <[hidden email]> wrote:
So function definitions are currently yanked to the top. This includes named functions.

However if a function is set to a variable, even if it is named, it is not yanked. Nor is the name exposed. My suggestion is that it is.

Namely, currently the following errors:
```js
module.exports.fnName2 = function fnName (){...}

fnName()
```

But I propose that it would not. Currently this can be gotten around by making it two lines, one to define the function and another to export it but that seems clunky 

Sebastian Malton


_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss



_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Yank Named and Assigned Functions

T.J. Crowder-2
In reply to this post by Sebastian Malton
On Wed, Mar 21, 2018 at 9:54 PM, Sebastian Malton <[hidden email]> wrote:
>
> ... it is not yanked. Nor is the name exposed. My suggestion is that it is.

That would be a massively breaking change. Not going to happen. :-) Consider:

```js
function foo(bar) {
    console.log(typeof bar);
    return function bar() { };
}
foo("");
```

Currently, that logs "string". With your proposal, it would log "function" just like this code does:

```js
function foo(bar) {
    console.log(typeof bar);
    function bar() { }
    return bar;
}
foo("");
```

...unless the hoisting rules were different for expressions than for declarations (which would be even more confusing than things already are).

Morever, there's a reason for the existing behavior. You'll have seen code like this:

```js
function Thing() {
}
Thing.prototype.foo = function foo() { /*...*/ };
Thing.prototype.bar = function bar() { /*...*/ };
```

We don't want those functions in historical code suddenly dumped into the scope where that code appears, potentially changing its meaning. (JScript used to do that, but it was fixed in IE9.)

If you want hoisting and an identifier in the scope, use a declaration:

```js
module.exports.fnName2 = fnName;
function fnName (){...}

fnName()
```

(And as Ben Newman points out, with `export` syntax you can export a function while declaring it by prefixing the declaration with `export`.)

-- T.J. Crowder

_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss