Yank Named and Assigned Functions
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:
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 9:54 PM, Sebastian Malton <sebastian at malton.name>
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:
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:
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:
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:
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
An HTML attachment was scrubbed... URL: esdiscuss/attachments/20180321/dbde3927/attachment