Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/420.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/35.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript var module=require()或const{module}=require()?_Javascript_Node.js_Node Modules_Require - Fatal编程技术网

Javascript var module=require()或const{module}=require()?

Javascript var module=require()或const{module}=require()?,javascript,node.js,node-modules,require,Javascript,Node.js,Node Modules,Require,这两种方法的区别是什么: 1. var xx = require('module') 2. const {xx} = require('module') 我看到了第一个可以作为变量访问xx的onde,所有脚本都由模块导出。。第二个xx未定义。如何访问第二个“方法”,或者它是一个构造模块而不能使用{}的方法 谢谢首先将完整的模块句柄放入名为xx的变量中 第二个从模块句柄获取xx属性,并将其放入名为xx的变量中。因此,第二种方法与: const xx = require('module').xx;

这两种方法的区别是什么:

1. var xx = require('module')
2. const {xx} = require('module')
我看到了第一个可以作为变量访问xx的onde,所有脚本都由模块导出。。第二个xx未定义。如何访问第二个“方法”,或者它是一个构造模块而不能使用{}的方法


谢谢

首先将完整的模块句柄放入名为
xx
的变量中

第二个从模块句柄获取
xx
属性,并将其放入名为
xx
的变量中。因此,第二种方法与:

const xx = require('module').xx;
const xx = require('module').xx;
另外,第一个是使用
var
,第二个是使用
const
,但我想您已经知道了这个区别


用另一种方式说:

这:

这是一个快捷方式:

const xx = require('module').xx;
在使用
require()
时,当您希望从模块中获取一组属性并将它们全部分配给模块中的顶级变量时,它是最有用的快捷方式,如下所示:

const {xx, yy, zz, aa, bb, cc} = require('module');
如果不使用对象解构语法,显然要比这一行复制更多的代码


仅供参考,所有这些都只是对象分解的一种形式(ES6中添加到Javascript中的功能)。对于
require()
,这并不是什么特别的问题,只是
require()
经常返回一个对象,其中包含一组感兴趣的属性。请参阅本文,了解对象解构的详细概述。

这是因为您的模块是作为对象导出的,而没有使用方法xxx。 例如:

  • var xx=require('module'),您可以通过以下方式调用函数abc:xxx.abc()
  • 只能通过const{abc}=require('module')调用abc 在案例2中不能调用xxx,因为没有名为xxx的方法

  • 这两者在两个方面有所不同:

  • xx
    :在第一种情况下,添加语句
    xx=5
    是合法的,但在第二种情况下不合法,因为您将xx声明为常量

  • 在第二个示例中,使用了分解结构。分解从对象中提取元素,并将其直接指定给变量。例如:

  • 然后,您的第二条语句只是从模块中获取
    xx
    属性,就好像您这样做了:

    首先,
    var
    是一种变量类型,定义后可以修改其值;
    const
    值在定义后不能修改

    其次,
    var xx
    指的是
    模块的主要出口
    const{xx}
    指的是
    模块的导出的属性


    示例1。模块

    const xx = {}
    module.exports = xx
    
    const xx = {}
    const yy = {}
    module.exports = {
      xx,
      yy,
    }
    
    const xx = {}
    export default xx
    
    const xx = {}
    const yy = {}
    export default {
      xx,
      yy,
    }
    
    示例2。模块

    const xx = {}
    module.exports = xx
    
    const xx = {}
    const yy = {}
    module.exports = {
      xx,
      yy,
    }
    
    const xx = {}
    export default xx
    
    const xx = {}
    const yy = {}
    export default {
      xx,
      yy,
    }
    
    否则


    示例1。模块

    const xx = {}
    module.exports = xx
    
    const xx = {}
    const yy = {}
    module.exports = {
      xx,
      yy,
    }
    
    const xx = {}
    export default xx
    
    const xx = {}
    const yy = {}
    export default {
      xx,
      yy,
    }
    

    示例2。模块

    const xx = {}
    module.exports = xx
    
    const xx = {}
    const yy = {}
    module.exports = {
      xx,
      yy,
    }
    
    const xx = {}
    export default xx
    
    const xx = {}
    const yy = {}
    export default {
      xx,
      yy,
    }
    

    do
    const xx=require('module')。您要做的是获取模块的
    xx
    字段<代码>常量{xx}=require('module')
    与const xx=require('module')相同。xx
    Nice!!!这意味着,如果我尝试使用“const{ricardo}=require('express'),console.log(ricardo)将是未定义的,因为express中没有类“ricardo”。如果生成一个文件“modal.js”=类test{}exports.test=test;还有一个“test.js”=const{test}=require(“./modal.js”)console.log(test),它返回[Function:test]!!解释得很好!!谢谢@里卡古格尔-是的,你现在似乎明白了。我建议阅读关于对象解构的一般知识,因为它是一种语言特性,在许多其他上下文中也很有用。是的!找到链接了!!阅读“对象解构”并测试所有内容!非常感谢!