This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var http = require('http'); //导入内置模块http | |
//调用内置模块http提供的createServer的API | |
http.createServer(function (req, res) { | |
res.writeHead(200, {'Content-Type': 'text/plain'}); | |
res.end('Hello World\n'); | |
}).listen(1337, '127.0.0.1'); | |
console.log('Server running at http://127.0.0.1:1337/'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var fs = require('fs'); | |
//异步读取Hello.txt | |
fs.readFile('Hello.txt','utf-8', | |
//回调函数里输出Hello.txt里的内容 | |
function(err, data){ | |
console.log(data); | |
} | |
); | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var fs = require('fs'); | |
//同步读取文件内容 | |
var data = fs.readFileSync('Hello.txt','utf-8'); | |
console.log(data); | |
console.log('End'); | |
/* | |
运行结果: | |
Hello World |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* JavaScript 单体模式 - 最基本结构的单体 */ | |
var Singleton = { | |
attribute1: "public attribute 1", | |
method1: function() { | |
console.log("This is public method1"); | |
// 不保险 若Singleton.method1作为一个事件监听器,那么this就会指向window | |
console.log(this.attribute1); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//父构造函数 | |
function CarMaker() { } | |
//所有子类通用方法 | |
CarMaker.prototype.drive = function () { | |
console.log( "Vroom, I have " + this.doors + " doors"); | |
}; | |
//静态工厂方法 子类无法继承 | |
CarMaker.factory = function(type) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//父构造函数 | |
function CarMaker() { } | |
//所有子类通用方法 | |
CarMaker.prototype.drive = function () { | |
console.log( "Vroom, I have " + this.doors + " doors"); | |
}; | |
//静态工厂方法 子类无法继承 | |
CarMaker.factory = function(type) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var SimpleHandler = function() { }; | |
SimpleHandler.prototype = { | |
request: function(method, url, callback, postVars) { | |
var xhr = this.createXhrObject(); | |
xhr.onreadystatechange = function() { | |
// do some things | |
}; | |
xhr.open(method. url, true); | |
xhr.send(postVars); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//旧的第三方代码 | |
function getSomething(str1, str2, str3) { | |
// do some thing | |
console.log("Old getSomething"); | |
return str1 + str2 + str3; | |
} | |
//自己的代码 | |
var str1 = "a", | |
str2 = "b", |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var getName = function(){ | |
return "Bob"; | |
} | |
var getSex = function(){ | |
return "man"; | |
} | |
var getUserInfo = function(){ | |
var info = getName() + getSex(); | |
return info; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var myevent = { | |
// ... | |
stop: function (e) { | |
//其他 | |
if(typeof e.preventDefault === "function") { | |
e.preventDefault(); | |
} | |
if(typeof e.stopPropagation === "function") { | |
e.stopPropagation(); | |
} |
OlderNewer