y0n1
y0n1

Reputation: 166

Why can't I export a function named "import" in ES6

I want to export a function I named "import" like this:

export function import(foo, bar) {
    console.log(foo + bar);
}

However for some reason the es6 linter complains that "import is not a valid identifier for a function" see this fiddle

What's wrong? Can't I have my function called import in es6? what about export?

Upvotes: 1

Views: 246

Answers (3)

Bergi
Bergi

Reputation: 664185

import and export are reserved words. You can't use them as the name of a function declaration.

You can however still use them as a name for your exports - you just can't declare a variable with it:

function _import(foo, bar) {
    console.log(foo + bar);
}
export {_import as import};

I would recommend against it though, it complicates importing similarly.

Upvotes: 4

hiddenuser.2524
hiddenuser.2524

Reputation: 4988

Because there are a lot of reserved words.

The spec says this:

An Identifier is an IdentifierName that is not a ReservedWord.

Here is a more comprehensive list of ReservedWords: https://mathiasbynens.be/notes/reserved-keywords .

Import, export and others are among them.

Upvotes: 2

Christoph
Christoph

Reputation: 51181

EcmaScript has a lot of reserved words, which are not valid as identifiers.

http://www.ecma-international.org/ecma-262/6.0/#sec-keywords gives you the complete list of words you are not allowed to use - and yes, export is also reserved.

Upvotes: 3

Related Questions