Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

View kampfer's full-sized avatar
🎯
Focusing

kampfer kampfer

🎯
Focusing
View GitHub Profile
@kampfer
kampfer / js.md
Last active December 18, 2015 05:39
js是一门动态的语言

1. 创建对象之后可以动态的添加删除属性和方法:

var man = new Man(); // 创建对象后再新增方法, 甚至可以覆盖删除原来的方法. // 静态语言无法办到, 使用类实例化对象之后, 对象无法修改. man.fuck = function() {};

2. 不是所有js程序都适合使用mvc

@kampfer
kampfer / klass.js
Last active December 15, 2015 23:09
Klass in <Javascript Patterns>
var klass = function(Parent, props) {
var Child, F, i;
//调用'子类'的构造函数时,'父类'的构造函数也会被调用
Child = function() {
if( Child.uber && Child.uber.hasOwnProperty('__construct') ) {
Child.uber.__construct.apply(this, arguments);
}
if( Child.prototype.hasOwnProperty('__construct') ) {
Child.prototype.__construct.apply(this, arguments);
@kampfer
kampfer / singleton.js
Created March 31, 2013 15:38
singleton pattern in javascript
var Single = (function(){
var instance;
return function() {
if(instance) {
return instance;
}
instance = this;
@kampfer
kampfer / binary_search.js
Created March 28, 2013 13:16
binary search in javascript
function binarySearch(collection, start, end, target) {
if(start > end) {
return;
}
var mid = Math.floor( (start + end) / 2 );
if(collection[mid] > target) {
return binarySearch(collection, start, mid - 1, target);
}
@kampfer
kampfer / css3animation.js
Last active December 14, 2015 16:19
helper js for css3 animation
var animation = {};
animation.prefix = '';
animation.transformName = '';
animation.animationName = '';
animation.perspectiveName = '';
@kampfer
kampfer / closure
Created March 6, 2013 07:21
a example about closure
function decorate(fn, decorator) {
return function() {
var args = Array.prototype.slice.apply(arguments);
args.unshift(fn);
return decorator.apply(null, args);
}
}
function total(x, y) {
return x + y;
//Class to be decorated
function Coffee(){
this.cost = function(){
return 1;
};
}
//Decorator A
function Milk(coffee){
this.cost = function(){
@kampfer
kampfer / form.js
Last active December 14, 2015 11:39
form&field
functon Form() {}
Form.prototype = {
addField : function(name, field) {},
getField : function(name) {},
getFieldAt : function(index) {},
removeField : function(name) {},
@kampfer
kampfer / composition.js
Created February 23, 2013 07:33
Composition Pattern In JavaScript
kampfer.require('events.EventTarget');
kampfer.provide('Composition');
kampfer.Composition = kampfer.events.EventTarget.extend({
_id : null,
_parent : null,
//array
@kampfer
kampfer / decorator.js
Last active December 14, 2015 02:19
Decorator Pattern In JavaScript
/**
* 装饰器模式在javascript中的实现方式
*
* 装饰器模式的优点:
* 1.动态的添加和删除职责
* 2.避免子类爆炸
* 3.避免在高层次结构的类中定义过多的特性
*
* 使用多个装饰器组合生成一个多功能的对象时,创建代码会相当长,
* 可以使用工厂模式处理这个问题.