exports和module.exports的区别

exports和module.exports的区别

我们今天主要来理解exports和module.exports的区别
写了一些代码,有问题请指教

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// file: app.js
var a = {name: 'yutiya'};
var b = a;

console.log(a);
console.log(b);

b.name = 'Yutiya';
console.log(a);
console.log(b);

b = {name: 'test'};

console.log(a);
console.log(b);

结果:

1
2
3
4
5
6
{ name: 'yutiya' }
{ name: 'yutiya' }
{ name: 'Yutiya' }
{ name: 'Yutiya' }
{ name: 'Yutiya' }
{ name: 'test' }

使用b变量修改属性,a变量的属性也会跟着变,说明两个变量使用一块内存,也就是说a和b都是指针(c和c++中)类型,指向同一个对象
给b变量赋值新的对象,b变量指向新的内存区域

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×