函数解析

exports与module.exports关系

1.基本类型,引用类型。
基本类型:保存在内存中简单数据段。按访问。
numberBealean,string,undefined,null
引用类型:保存在堆内存中的对象。也就是保存的是一个指针,指针指向对象在内存中的位置。按引用访问。
function,object
如果对引用类型重新赋值,则会断开引用。
nodeJS模块中module.exports与module的区别。

  • module.exports 初始值为一个空对象 {}
  • exports 是指向的 module.exports 的引用
  • require() 返回的是 module.exports 而不是 exports
let a = something;
exports = module.exports = a
//等价
module.exports = somethings
//此时断开exports对module.exports的应用。
exports = module.exports
//重新指向。

new 操作符

1.如果是构造函数。

var Person = function(name){
    this.name = name;
    this.say = function(){
        return "I am " + this.name;
    };
}
var nyf = new Person("dl");
nyf.say();

执行

1、创建一个空对象,并且 this 变量引用该对象,同时还继承了该函数的原型。
2、属性和方法被加入到 this 引用的对象中。
3、新创建的对象由 this 所引用,并且最后隐式的返回 this 。

2.如果是普通函数
则与直接调用该函数结果相同。

你可能感兴趣的:(函数解析)