Home >Web Front-end >Front-end Q&A >What is the usage of export and as in es6

What is the usage of export and as in es6

WBOY
WBOYOriginal
2022-03-31 14:50:144275browse

In es6, the export keyword is used to modularize variables, functions, and objects so that they can be referenced externally and provide an external calling interface. The syntax is "export var a = 'name';"; as key The word is used to modify the variable name, and the syntax is "export {new variable name as old variable name}".

What is the usage of export and as in es6

The operating environment of this tutorial: Windows 10 system, ECMAScript version 6.0, Dell G3 computer.

Usage of export and as in es6

Usage of export:

export allows us to export variables, functions, The object is modularized and provides an external calling interface for external reference. Let’s look at the simplest example first, modularizing a variable. We create a new temp.js file and output a module variable in the file.

export var a = 'jspang';

Then it can be introduced in the form of import in index.js.

import {a} from './temp.js';
console.log(a);

cmd command module input

babel-node index.js Pay attention to the directory

Multi-variable output (temp.js)

var a ='jspang';
var b ='技术胖';
var c = 'web';
export {a,b,c} //包装成对象{}

Modular output of function (temp.js)

export function add(a,b){
    return a+b;
}

as usage

Sometimes we don’t want to expose the inside of the module variable name, and give the module a more semantic name, then we can use as to operate.

var a ='jspang';
var b ='技术胖';
var c = 'web';
export {
    x as a,
    y as b,
    z as c
}

When you may encounter duplicate variable names or need to modify the variable names during development, you can use the keyword as to rename them with one click in ES6.

What is the usage of export and as in es6

[Related recommendations: javascript video tutorial, web front-end

The above is the detailed content of What is the usage of export and as in es6. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn