“什么是”呢;“导出默认值”;用JavaScript?

“什么是”呢;“导出默认值”;用JavaScript?,javascript,node.js,ecmascript-6,Javascript,Node.js,Ecmascript 6,文件: 我以前从未见过导出默认值。对于导出默认值,是否有更容易理解的等效内容?它是ES6模块系统的一部分。该文档中还有一个有用的示例: 如果模块定义了默认导出: // foo.js export default function() { console.log("hello!") } 然后,可以通过省略大括号导入该默认导出: import foo from "foo"; foo(); // hello! 更新:自2015年6月起,在ECMAScrip

文件:


我以前从未见过导出默认值。对于导出默认值,是否有更容易理解的等效内容?

它是ES6模块系统的一部分。该文档中还有一个有用的示例:

如果模块定义了默认导出:

// foo.js
export default function() { console.log("hello!") }
然后,可以通过省略大括号导入该默认导出:

import foo from "foo";
foo(); // hello!


更新:自2015年6月起,在ECMAScript 2015规范中定义了模块系统,特别是在中定义了
导出
语法。

导出默认函数(){}
可在函数没有名称时使用。一个文件中只能有一个默认导出。另一种方法是命名导出


详细介绍导出默认值,以及有关我认为非常有用的模块的其他详细信息。

导出默认值
用于从脚本文件导出单个类、函数或原语

导出也可以写为

export default function SafeString(string) {
  this.string = string;
}

SafeString.prototype.toString = function() {
  return "" + this.string;
};
这用于在另一个脚本文件中导入此函数

比如在app.js中,你可以

import SafeString from './handlebars/safe-string';
关于出口 顾名思义,它用于从脚本文件或模块导出函数、对象、类或表达式

Utilities.js

这可以导入并用作

App.js

使用导出默认值时,这要简单得多。脚本文件只导出一件事。 cube.js

用作 App.js

正如上面所解释的

有两种不同类型的导出,命名导出和默认导出。你可以 每个模块有多个命名导出,但只有一个默认值 导出[…]命名的导出可用于导出多个值。在期间 在导入时,必须使用对应的相同名称 对象。但是可以使用任何名称导入默认导出

例如:

let myVar; export default myVar = 123; // in file my-module.js

import myExportedVar from './my-module' //  we have the freedom to use 'import myExportedVar' instead of 'import myVar' because myVar was defined as default export

console.log(myExportedVar);        // will log 123
在我看来,默认导出的重要一点是它可以用任何名称导入

如果有一个文件foo.js,它将导出默认值:

导出默认函数foo(){}
JavaScript中的“导出默认值”是什么

在默认导出中,导入的命名是完全独立的,我们可以使用我们喜欢的任何名称。

我将用一个简单的例子来说明这一点

假设我们有三个模块和一个index.html文件:

  • modul.js
  • 模块2.js
  • 模块3.js
  • index.html
文件modul.js 文件module2.js 模块3.js

export default function hello3() {
    console.log("Module3: Saying hello for the third time!");
}
文件index.html
因此较长的解释是:

如果要为模块导出单个内容,则使用“导出默认值”

因此,重要的是“importblablafrom./module3.js”-我们可以说:

“从“./module3.js”导入pamelanderson,然后导入
pamelanderson()。当我们使用“导出默认值”时,这将很好地工作,基本上就是这样-它允许我们在默认值时随意命名它


注意:如果您想测试示例-首先创建文件,然后在浏览器中允许->如果您在浏览器的URL中使用Firefox,请键入:about:config->搜索“privacy.file\u unique\u origin”->将其更改为“false”->打开index.html->按F12打开控制台并查看输出->享受,不要忘记将CORS设置返回默认值

p.S.2:对于愚蠢的变量命名,我很抱歉


详细信息在和中。

导出默认值用于导出单个类、函数或原语


导出默认值function(){}可在函数没有名称时使用。一个文件中只能有一个默认导出

有两种不同类型的导出,命名默认。每个模块可以有多个命名导出,但只能有一个默认导出。每种类型对应于上述其中一种。

命名导出

export class NamedExport1 { }

export class NamedExport2 { }

// Import class
import { NamedExport1 } from 'path-to-file'
import { NamedExport2 } from 'path-to-file'

// OR you can import all at once
import * as namedExports from 'path-to-file'
export default class DefaultExport1 { }

// Import class
import DefaultExport1 from 'path-to-file' // No curly braces - {}
默认导出

export class NamedExport1 { }

export class NamedExport2 { }

// Import class
import { NamedExport1 } from 'path-to-file'
import { NamedExport2 } from 'path-to-file'

// OR you can import all at once
import * as namedExports from 'path-to-file'
export default class DefaultExport1 { }

// Import class
import DefaultExport1 from 'path-to-file' // No curly braces - {}
//可以为默认导入使用其他名称

import Foo from 'path-to-file' // This will assign any default export to Foo.

导出默认值用于从文件中仅导出一个值,该文件可以是类、函数或对象。可以使用任何名称导入默认导出

//file functions.js

export default function subtract(x, y) {
  return x - y;
}

//importing subtract in another file in the same directory
import myDefault from "./functions.js";
在导入的文件中,减法函数可以称为myDefault

“导出默认值”还会创建一个回退值,这意味着如果尝试导入命名导出中不存在的函数、类或对象,则返回该值。将提供默认导出时给定的回退值


详细的解释可以在

@geenhank上找到,我想这是意料之中的,因为ES6仍然是一个草案。我提供了一个更新的链接和一个免责声明。我看不出export default function(){}与export=function(){}有什么不同……在类似于
export const Foo=()=>{}
的情况下,然后在文件的末尾
export default Foo
我在一系列示例中看到了这一点。这两个导出是怎么回事?如果能看到一个包含默认导出和命名导出的示例,那就太好了。换言之,这种导出将满足“/foo”的
import foo、{bar,baz}谢谢你的回答,但是第二个例子中foo的用法有点模棱两可;什么是foo,你如何命名第一个文件;你怎么能从“foo”导入foo呢?是否有一个对象包含foo,因为在第一个示例中,导出的函数未命名@p、 如果愿意,s.w.gYou可以同时使用默认导出和命名导出。@Greg gum页面已过时。它正在重定向到@rajakvk,没错,但是原始页面有更多的背景信息供初学者使用。这个答案比公认的答案要好,因为它解释了
default
的意思,而对我来说,这个问题是关于这个词的。@DariuszSikorski公认的答案解释了
default
的意思
export function hello2() {
    console.log("Module2: Saying hello for the second time!");
}

export let variable2 = 456;
export default function hello3() {
    console.log("Module3: Saying hello for the third time!");
}
<script type="module">
import * as mod from './modul.js';
import {hello2, variable2} from './modul2.js';
import blabla from './modul3.js'; // ! Here is the important stuff - we name the variable for the module as we like

mod.hello();
console.log("Module: " + mod.variable);

hello2();
console.log("Module2: " + variable2);

blabla();
</script>
modul.js:2:10   -> Modul: Saying hello!
index.html:7:9  -> Module: 123
modul2.js:2:10  -> Module2: Saying hello for the second time!
index.html:10:9 -> Module2: 456
modul3.js:2:10  -> Module3: Saying hello for the third time!
export class NamedExport1 { }

export class NamedExport2 { }

// Import class
import { NamedExport1 } from 'path-to-file'
import { NamedExport2 } from 'path-to-file'

// OR you can import all at once
import * as namedExports from 'path-to-file'
export default class DefaultExport1 { }

// Import class
import DefaultExport1 from 'path-to-file' // No curly braces - {}
import Foo from 'path-to-file' // This will assign any default export to Foo.
//file functions.js

export default function subtract(x, y) {
  return x - y;
}

//importing subtract in another file in the same directory
import myDefault from "./functions.js";