node的模块与引用
module.js
//直接封装函数,可以在引用的js处调用
exports.
world =
function(){
console.
log(
'hello')
}
//只是把一个对象封装到模块中:
function
hello(){
var
name =
'111'
this.
setName =
function(
newName){
name =
newName
}
this.
sayHello =
function(){
console.
log(
'hello'+
name)
}
}
module.
exports =
hello;
require.js
//引入模块
//node遵循commonJs语法,不能用import和define
var
hello =
require (
'./module');
console.
log(
hello)
//实例化一个hello对象,不实例化无法应用
var
hell =
new
hello();
hell.
setName(
'222');
hell.
sayHello()
//Node.js函数
function
xx(
someFunction,
someValue){
someFunction(
someValue)
}
//第一个参数是某函数,第二个是传递的值
xx(
hell.
setName,
'444')
xx(
hell.
sayHello)
//匿名函数
xx(
function(
word){
console.
log(
word)
},
'eee')