JS中exports与module.exports的区别详细介绍

JavaScript017

JS中exports与module.exports的区别详细介绍,第1张

exports和module.exports 都是对外暴露对象接口的,但是使用需要注意以下细节:

例如:这是rocker.js文件

在另一个文件中你这样引用

其实, Module.exports 才是真正的接口, exports 只不过是它的一个辅助工具。最终返回给调用的是 Module.exports 而不是 exports 。

注意:

所有的exports收集到的属性和方法,都赋值给了Module.exports。当然,这有个前提,就是Module.exports本身不具备任何属性和方法。如果,Module.exports已经具备一些属性和方法,那么exports收集来的信息将被忽略。

例如:

会以对象(也可叫字典)输出

如果你想在module.js中使用outputA或outputB函数,就得

上面的写法也等同于

exports收集到的属性和方法,并未都赋值给了Module.exports,因为Module.exports已经具备一些属性和方法,因此exports收集来的信息将 被忽略

再次引用执行rocker.js

下面例子中,你的模块是一个类: 想导出的模块为特定的数组类型

可以这样应用它:

下面例子中,你的模块是一个数组: 想导出的模块为特定的数组类型

可以这样应用它

结论: 现在你明白了,如果你 想你的模块是一个特定的类型 就用Module.exports。如果你想的模块是一个典型的“实例化对象”就用exports。

1.首先需要新建并使用自己的packageManager

2.在你的Activitiy或Fragment中对其进行设置

3.Step by Step[按照文档指定步骤进行操作]:

1.Create the ViewManager subclass.

2.Annotate the view properties with @UIProp

3.Implement the createViewInstance method

4.Implement the updateView method

5.Register the manager in createViewManagers of the applications package.

6.Implement the JavaScript module

4.以新增一个自定义Toast为例

nodejs的几种模块加载方式

一.直接在exports对象中添加方法

1. 首先创建一个模块(module.js)module.js

exports.One = function(){

console.log('first module')

}

2.load.jsvar module =require('./module')

module.One()

这样我们就可以在引入了该模块后,返回一个exports对象,这里是指module对象,其实都只是两个引用或者句柄,只是都指向了同一个资源,在load.js里,module的名字可以是任意取的,因为它仅仅是指向require('./module')返回后的一个实例对象的引用,在load.js文件里的module和在module.js里的exports对象是同一个东西.因此上述两个文件可以用一个文件来表示:exports.One = function(){

console.log('first module')

}

exports.One()

其运行结果是一致的,这里我们可以很清晰的看到,我们在使用require('./xxxx')后其实返回的总是在 xxxx.js文件中的exports对象的引用,这个引用的名字我们可以任意取,但是为了规范我们还是最好取符号某些非标准规定(后面说道),但是这样会有不妥的地方,因为它是始终指向exports的实例对象,也就是说,我们虽然有了这个模块,但是这个模块我们只能使用一次,这取决于rquire('./module')只会加在一次该模块.比如我们修改上述代码,

module.js

var name

exports.setName = function(oName){

name = oName

}

exports.getName = function(){

console.log(name)

}

load.jsvar module1 = require('./module')

module1.setName("felayman1")

module1.getName()

var module2 = require('./module')

module2.setName("felayman2")

module2.getName()

module1.getName()

我们可以看到,虽然我们使用了两次require('./module'),但是当我们修改module2后,module1的内容也被修改,这恰恰说明了,module1和module2是指向的同一个对象.有时候这并不影响我们的程序,但是如果我们的module是Person呢?我们希望我们require('./person')后返回的是不同的对象.因此,这种方式是有缺陷的,尽管很方便,这种方式在大部分nodejs的模块中都是很常见,比如fs模块,http模块等.

二.将模块中的函数挂载到exports对象的属性上

person.js

function Person{

var name

this.setName = function(theName){

name = theName

}

this.sayHello = function(){

console.log('Hello',name)

}

}

exports.Person = Person

load.js

var Person = require('./person').Person

var person1 = new Person()

person1.setName("felayman1")

person1.sayHello()

var person2 = new Person()

person2.setName("felayman2")

person2.sayHello()

person1.sayHello()

这样我们可以看到,我们就可以引入一个函数了,我们把在person.js文件中的Person函数设置为eports对象的一个属性,我们只需要在load.js文件中引入该属性,就可以获取到多个该函数的实例,在nodejs中的EventEmitter就是基于这种方式,但是这样我们总是在使用 require('./person').Person这样的写法有点太复杂,因此nodejs允许我们使用其他更简洁的方式,利用全局变量--module,这样我们在其他文件中引入其他模块的时候,就更方便了.

三.利用全局变量module

person.js

function Person(){

var name

this.setName = function(theName){

name = theName

}

this.sayHello = function(){

console.log('Hello',name)

}

}

// exports.Person = Person

module.exports = Person

load.jsvar Person = require('./person')

var person1 = new Person()

person1.setName("felayman1")

person1.sayHello()

var person2 = new Person()

person2.setName("felayman2")

person2.sayHello()

person1.sayHello()

这样一修改,我们就在使用require函数的时候就方便了,如果觉得这里难以理解,我们可以把两个文件里语法放到一起:var Person = require('./person')

module.exports = Person

这样,我们就可以看出,其实就是这样var Person = Person.

因为上述我们都已经说过,require('./person')其实就是module.exports 对象的,这里的module我们不用太在意,就跟javascript中的window一样,是一个全局变量,即 module.exports =exports就类似于window.alert() =alert()差不多的效果,这样我们就能看出,我们再次使用require('./person')的时候其实就是导入了我们所需要的exports对象的属性函数模板了,这样我们也可以多次实例化我们所需要的对象了.这种方式是综合了前两种的方法,因此也是官方推荐的使用方法.