var man = new Man(); // 创建对象后再新增方法, 甚至可以覆盖删除原来的方法. // 静态语言无法办到, 使用类实例化对象之后, 对象无法修改. man.fuck = function() {};
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 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); |
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 Single = (function(){ | |
var instance; | |
return function() { | |
if(instance) { | |
return instance; | |
} | |
instance = this; |
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 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); | |
} |
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 animation = {}; | |
animation.prefix = ''; | |
animation.transformName = ''; | |
animation.animationName = ''; | |
animation.perspectiveName = ''; |
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 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; |
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
//Class to be decorated | |
function Coffee(){ | |
this.cost = function(){ | |
return 1; | |
}; | |
} | |
//Decorator A | |
function Milk(coffee){ | |
this.cost = function(){ |
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
functon Form() {} | |
Form.prototype = { | |
addField : function(name, field) {}, | |
getField : function(name) {}, | |
getFieldAt : function(index) {}, | |
removeField : function(name) {}, |
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
kampfer.require('events.EventTarget'); | |
kampfer.provide('Composition'); | |
kampfer.Composition = kampfer.events.EventTarget.extend({ | |
_id : null, | |
_parent : null, | |
//array |
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中的实现方式 | |
* | |
* 装饰器模式的优点: | |
* 1.动态的添加和删除职责 | |
* 2.避免子类爆炸 | |
* 3.避免在高层次结构的类中定义过多的特性 | |
* | |
* 使用多个装饰器组合生成一个多功能的对象时,创建代码会相当长, | |
* 可以使用工厂模式处理这个问题. |