Skip to content

Instantly share code, notes, and snippets.

View KJlmfe's full-sized avatar

KJlmfe KJlmfe

  • China
View GitHub Profile
@KJlmfe
KJlmfe / node_use_module_example.js
Last active August 2, 2016 06:29
Node内置核心模块与第三方模块使用举例
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/');
@KJlmfe
KJlmfe / async_node.js
Last active August 2, 2016 06:29
Node异步执行
var fs = require('fs');
//异步读取Hello.txt
fs.readFile('Hello.txt','utf-8',
//回调函数里输出Hello.txt里的内容
function(err, data){
console.log(data);
}
);
@KJlmfe
KJlmfe / sync_node.js
Created January 10, 2014 03:00
Node同步执行
var fs = require('fs');
//同步读取文件内容
var data = fs.readFileSync('Hello.txt','utf-8');
console.log(data);
console.log('End');
/*
运行结果:
Hello World
@KJlmfe
KJlmfe / javascript-basic-singleton-pattern.js
Last active August 29, 2015 14:00
JavaScript 单体模式-最基本结构的单体
/* JavaScript 单体模式 - 最基本结构的单体 */
var Singleton = {
attribute1: "public attribute 1",
method1: function() {
console.log("This is public method1");
// 不保险 若Singleton.method1作为一个事件监听器,那么this就会指向window
console.log(this.attribute1);
@KJlmfe
KJlmfe / javascript-factory-pattern-example1.js
Created April 24, 2014 04:13
JavaScript 设计模式 - Factory(工厂模式) 示例1
//父构造函数
function CarMaker() { }
//所有子类通用方法
CarMaker.prototype.drive = function () {
console.log( "Vroom, I have " + this.doors + " doors");
};
//静态工厂方法 子类无法继承
CarMaker.factory = function(type) {
@KJlmfe
KJlmfe / javascript-factory-pattern-example1.js
Created April 24, 2014 06:44
JavaScript 设计模式 - Factory(工厂模式) 示例1
//父构造函数
function CarMaker() { }
//所有子类通用方法
CarMaker.prototype.drive = function () {
console.log( "Vroom, I have " + this.doors + " doors");
};
//静态工厂方法 子类无法继承
CarMaker.factory = function(type) {
@KJlmfe
KJlmfe / javascript-factory-xhr.js
Created April 24, 2014 06:46
JavaScript 设计模式 - Factory(工厂模式) - 运行时确定创建对象
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);
@KJlmfe
KJlmfe / javascript-adapter-pattern-1.js
Created April 25, 2014 02:52
JavaScript 设计模式 - Adapter(适配器模式)-1
//旧的第三方代码
function getSomething(str1, str2, str3) {
// do some thing
console.log("Old getSomething");
return str1 + str2 + str3;
}
//自己的代码
var str1 = "a",
str2 = "b",
@KJlmfe
KJlmfe / javascript-facade-pattern-1.js
Last active August 29, 2015 14:00
JavaScript 设计模式 - Facade(外观模式/门面模式)-组合接口
var getName = function(){
return "Bob";
}
var getSex = function(){
return "man";
}
var getUserInfo = function(){
var info = getName() + getSex();
return info;
@KJlmfe
KJlmfe / javascript-facade-pattern-2.js
Created April 26, 2014 02:06
JavaScript 设计模式 - Facade(外观模式/门面模式)-封装浏览器差异
var myevent = {
// ...
stop: function (e) {
//其他
if(typeof e.preventDefault === "function") {
e.preventDefault();
}
if(typeof e.stopPropagation === "function") {
e.stopPropagation();
}