首页 > 编程知识 正文

module.js源码(html module)

时间:2023-12-02 21:07:34 阅读:311613 作者:AQVY

本文目录一览:

  • 1、nodejs 运行错误:module.js:340 throw err;是什么原因
  • 2、如何加载Nodejs模块
  • 3、为什么nodejs的module.js里用了readFileSync而不用readFile
  • 4、node.js的module.export 和 export方法的区别
  • 5、cocos creator怎么写逻辑

nodejs 运行错误:module.js:340 throw err;是什么原因

复制网上一个教程里的一段代码,一个简易服务器,但运行不起来,不知道是什么原因。我把运行结果和代码都贴出来,望解答。

代码:

var http = require("http");

http.createServer(function(request, response) {

response.writeHead(200, {"Content-Type": "text/plain"});

response.write("Hello World");

response.end();

}).listen(8888);

运行结果:

D:testnode server.js

module.js:340

throw err;

^

Error: Cannot find module 'D:testserver.js'

at Function.Module._resolveFilename (module.js:338:15)

at Function.Module._load (module.js:280:25)

at Function.Module.runMain (module.js:497:10)

at startup (node.js:119:16)

at node.js:906:3

如何加载Nodejs模块

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对象的属性函数模板了,这样我们也可以多次实例化我们所需要的对象了.这种方式是综合了前两种的方法,因此也是官方推荐的使用方法.

为什么nodejs的module.js里用了readFileSync而不用readFile

进一步说,之所以同步是 Node.js 所遵循的 CommonJS 的模块规范要求的。

在当年,CommonJS 社区对此就有很多争议,导致了坚持异步的 AMD 从 CommonJS 中分裂出来。

CommonJS

模块是同步加载和同步执行,AMD 模块是异步加载和异步执行,CMD(Sea.js)模块是异步加载和同步执行。ES6

的模块体系最后选择的是异步加载和同步执行。也就是 Sea.js 的行为是最接近 ES6 模块的。不过 Sea.js

这样做是需要付出代价的——需要扫描代码提取依赖,所以它不像 CommonJS/AMD 是纯运行时的模块系统。

注意 Sea.js 是

2010年之后开发的,提出 CMD 更晚。Node.js 当年(2009年)只有 CommonJS 和 AMD 两个选择。就算当时已经有

CMD 的等价提案,从性能角度出发,Node.js 不太可能选择需要静态分析开销的 类 CMD 方案。考虑到 Node.js

的模块是来自于本地文件系统,最后 Node.js 选择了看上去更简单的 CommonJS 模块规范,直到今天。

node.js的module.export 和 export方法的区别

node.js中module.export与export的区别。

可能是有史以来最简单通俗易懂的有关Module.exports和exports区别的文章了。

exports = module.exports = {}

所以module.exports和exports的区别就是var a={}; var b=a;,a和b的区别

看起来木有什么太大区别,但实际用起来的时候却又有区别,这是为啥呢,请听我细细道来

关于Module.exports和exports有什么区别,网上一搜一大把,但是说的都太复杂了…听说exports是Module.exports对象的一个引用(reference)^1,什么是引用?!…_(:з」∠)_

当然啦,如果要彻底理解这两个导出的区别,最好肯定是去看源码,看看都是怎么封装的,功力深厚的童鞋应该一看就懂了。不过,源码我也是看不懂的…(ಥ_ಥ)

但是最近感觉杂七杂八看了好多文章做了好多实验之后,像是打开了任督二脉,机智的我好像有点上道了…

首先要明确的一点,module是一个对象 {Object}。当你新建一个文件,比如mo.js,文件内容如下:

1 console.log(module)

然后在CMD里执行这个文件node mo.js,就能看到module其实是一个Module实例,你可以这么理解,NodeJS中定义了一个Module类,这个类中有很多属性和方法,exports是其中的一个属性:

12345 function Module { id : 'blabla', exports : {}, blabla...}

当每个js文件在执行或被require的时候,NodeJS其实创建了一个新的实例var module = new Module(),这个实例名叫module。这也就是为什么你并没有定义module这个变量,却能console.log出来而不会报错的原因。

假设我有一个JS文件内容如下:

有了上面的基础,很容易理解module.export其实是给Module实例中的exports对象中添加方法/属性。

通常使用exports的时候,是这么用的:

假设我有一个JS文件内容如下:

由此也能看出,传说中的exports其实是module.exports的引用,你可以这么理解,NodeJS在你的代码之前悄悄的加了以下代码:

这也就是为什么你并没有定义exports这个变量,却能console.log出来而不会报错的原因。

当你从外部调用某个模块,require其实是在require什么?^2require的时候NodeJS会到处去找有没有这个模块,如果有,return的就是module.exports里的东东。

cocos creator怎么写逻辑

主要两个js源码:

HelloWorld.js

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

cc.Class({

extends: cc.Component,

properties: {

label: {

default: null,

type: cc.Label

},

text: 'Hello, World!',

t_prefab:{

default:null,

type:cc.Prefab

},

t_sprite:{//定义一个cc的类型,并定义上常用属性

default:null,

type:cc.SpriteFrame,//类型的定义

// url:cc.Texture2D, //Raw Asset(cc.Texture2D, cc.Font, cc.AudioClip)

visible:true,//属性检查器中是否可见

displayName:'himi',//属性检查器中属性的名字

tooltip:"测试脚本",//属性检查器中停留此属性名称显示的提示文字

readonly:false,//属性检查器中显示(readonly)且不可修改[当前有bug,设定只读也能修改]

serializable:true,//设置false就是临时变量

editorOnly:false//导出项目前剔除此属性

},

t_url:{

default:null,

url:cc.Texture2D

},

t_count_2:200,//基础类型

//可以只定义 get 方法,这样相当于一份 readonly 的属性。[当前有bug,只设定get也能修改]

t_getSet:{

default:12,

get:function(){return this.t_getSet},//get

set:function(value){this.t_getSet =value;}//set

},

t_array:{//定义一个数组

default:[],

type:[cc.Sprite]

}

},

// use this for initialization

onLoad: function () {

//--- 获取组件的几种形式:

//1. 通过属性检查器被赋值的label组件,直接拿到得到实例

//2. 通过属性检查器被赋值的label组件所在的node节点,然后通过getComponent获取

// this.label.string = this.text;

//3. 获取当前this(node)节点上的label组件

// var _label = this.getComponent(cc.Label);

//4. 先获取目标组件所在的节点,然后通过getComponent获取目标组件

var _label = cc.find("Canvas/label").getComponent(cc.Label);

//5.也可以如下形式【注意此种方式,目前有BUG,无法正常使用 (0.7.1) 】

// var _label = cc.find("Canvas/labelcc.Label");

console.log(_label.string);

console.log(this.t_getSet);

//---全局变量的访问

/* 任意脚本中定义如下:【注意不要有var哦】

t_global = {

tw:100,

th:200

};

*/

t_global.th = 2000;

console.log(t_global.th);

//---模块之间的访问

/*任意脚本中定义如下 【注意关键字是module.exports】

module.exports= {

tme_pa1:"100",

tme_pa2:333221

};

*/

//---用 require + 文件名(不含路径) 来获取到其他 模块 的对象

var tModuleData = require("testJs");

tModuleData.tme_pa2 = 991;

console.log(tModuleData.tme_pa2);

//---在当前节点下添加一个组件

var mySprite = new cc.Node().addComponent(cc.Sprite);

mySprite.spriteFrame = this.t_sprite;

mySprite.node.parent = this.node;

mySprite.node.setPosition(300,200);

//---复制节点/或者复制 prefab

//复制节点

var lLabel = cc.instantiate(this.label);

lLabel.node.parent = this.node;

lLabel.node.setPosition(-200,0);

//复制prefab

var tPrefab = cc.instantiate(this.t_prefab);

tPrefab.parent = this.node;

tPrefab.setPosition(-210,100);

//--- 销毁节点(销毁节点并不会立刻发生,而是在当前 帧逻辑更新结束后,统一执行)

if (cc.isValid(this.label.node) ) {

console.log("有效存在,进行摧毁");

this.label.destroy();

}else{

console.log("已摧毁");

}

//--- 事件监听 on 4种形式

//枚举类型注册

var tFun =function (event){

console.log("touchend event:"+event.touch.getLocation().x +"|"+event.touch.getLocation().y);

};

this.node.on(cc.Node.EventType.TOUCH_END,tFun,this);

//事件名注册

// var tFun =function (event){

// console.log("touchend event");

// };

// this.node.on("touchend",tFun);

// this.node.on("touchend",function (event){

// console.log("touchend event");

// });

// this.node.on("touchend",function (event){

// console.log("touchend event");

// },this);

// this.node.on("touchend",function (event){

// console.log("touchend event");

// }.bind(this));

//--- 一次性的事件监听 once

// this.node.once("touchend",function (event){

// console.log("touchend event");

// });

//--- 关闭监听

this.node.off("touchend",tFun,this);

//--- 发射事件(事件手动触发)

this.node.on("tEmitFun",function (event){

console.log("tEmitFun event:"+event.detail.himi+"|"+event.detail.say);

//-- 事件中断,如下函数阻止事件向当前父级进行事件传递

// event.stopPropagation();

});

this.node.emit("tEmitFun",{himi:27,say:"hello,cc!"});

//--- 动作,类似c2dx api 基本无变化

var mTo = cc.moveBy(1,-100, -200);

var mAction = cc.repeatForever(cc.sequence(cc.moveBy(1,-100, -200),mTo.reverse(),cc.delayTime(0.5),cc.callFunc(function(action,data){

console.log("action callback:"+data.himi);

},this,{tx:100,himi:"i'm action callback and bring data"})));

mySprite.node.runAction(mAction);

//暂停动作

mySprite.node.stopAction(mAction);

//--- 计时器 (component)schedule (cc.Node 不包含计时器相关 API)

//参数: call funtion/interval/repeat times/delay time

//不延迟,永久重复

this.schedule(function(){

console.log("schedule log...");

},1);

//不延迟,有重复次数限定

// this.schedule(function(){

// console.log("schedule log...");

// },1,2);

//重复2次,重复间隔为1秒,延迟1秒进行

// this.schedule(function(){

// console.log("schedule log...");

// },1,2,1);

//一次性的计时器

var mySch =function(){ console.log("schedule Once log..."); }

this.scheduleOnce(mySch);

//取消定时器

this.unschedule(mySch);

//--- url raw资源获取

var mSf = new cc.Node().addComponent(cc.Sprite);

mSf.spriteFrame = this.t_sprite;

mSf.spriteFrame.setTexture(this.t_url);

mSf.node.setPosition(400,0);

mSf.node.parent = this.node;

mSf.node.setScale(0.5);

//获得 Raw Asset 的 url

var mUrl = cc.textureCache.addImage(cc.url.raw("himi.png"));

console.log("raw asset url:"+mUrl);

},

// called every frame

update: function (dt) {

// if (cc.isValid(this.label.node) ) {

// console.log("有效存在,进行摧毁");

// }else{

// console.log("已摧毁");

// }

},

});

testJs.js

1

2

3

4

5

6

7

8

9

t_global = {

tw:100,

th:200

};

module.exports= {

tme_pa1:"100",

tme_pa2:333221

};

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。