Skip to content

Instantly share code, notes, and snippets.

View KJlmfe's full-sized avatar

KJlmfe KJlmfe

  • China
View GitHub Profile
@KJlmfe
KJlmfe / LoveRose.sol
Created February 14, 2018 14:06
LoveRose Contract
pragma solidity ^ 0.4.17;
contract LoveRose {
address public ceoAddress;
struct Rose {
uint64 birthTime;
address buyer;
uint value;
@KJlmfe
KJlmfe / javascript-iterator-array.js
Created April 26, 2014 03:42
JavaScript 设计模式 - Iterator(迭代器模式) - 数组示例
/* 迭代器示例 */
var agg = (function() {
var index = 0,
data = [1,2,3,4,5,6];
length = data.length;
var api = {
next: function() {
if(!this.hasNext()) {
@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();
}
@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-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-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-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-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-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 / 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