Skip to content

Instantly share code, notes, and snippets.

View e7h4n's full-sized avatar
🎯
Focusing

Ethan Zhang e7h4n

🎯
Focusing
View GitHub Profile
" 最前面加入
call pathogen#runtime_append_all_bundles()
call pathogen#helptags()
runtime! config.vim
runtime! handy.vim
runtime! shortcut.vim
runtime! cfg/zencoding.vim
runtime! cfg/js.vim
runtime! cfg/nerdtree.vim
" 最前面加入
call pathogen#runtime_append_all_bundles()
call pathogen#helptags()
runtime! config.vim
runtime! handy.vim
runtime! shortcut.vim
runtime! cfg/zencoding.vim
runtime! cfg/js.vim
" 最前面加入
call pathogen#runtime_append_all_bundles()
call pathogen#helptags()
runtime! config.vim
runtime! handy.vim
runtime! shortcut.vim
runtime! cfg/zencoding.vim
runtime! cfg/js.vim
runtime! cfg/nerdtree.vim
alert(true);

何为契约式设计 (Design by Contract)

在面向对象编程中,复用性是非常重要的概念。为了保证各个模块的高度复用性,我们需要一个系统的方法来描述模块以及模块在系统中的关系。比如每个模块的说明文档,指出了模块的输入格式输入格式,就是这样一种系统的方法。契约式设计也提供了这样一个系统的方法,在契约式设计的理论中,一个软件系统被视为一组相互交流的部件,它们之间互动的基础是一组准确定义了的规范,它规定了这些部件相互之间的义务 -- 即契约。

例子

这里将以一个普通的除法函数作为开始,一步步的说明如何在 JavaScript 里面使用契约式设计。

/**

*@method divide

  • 使命召唤 4 现代战争
  • 鹰击长空 2
  • 龙珠 怒气爆发 2
  • 细胞分裂
  • 模拟人生 3
  • 极品飞车 14
  • 实况足球 2011
  • 刺客信条兄弟会
  • 生化危机 5
  • 寂静岭 归乡
alert(true);
var currentEditObj = null;
function findParent(obj, filter){
while (obj.nodeType !== Node.DOCUMENT_NODE) {
if (filter(obj)) {
return obj;
}
obj = obj.parentNode;
}
return null;
var foo = {
bind: function() {},
trigger: function() {}
};
foo.bind('fooEvent', function() {
alert('a');
}); // Nothing will be alerted
foo.trigger('fooEvent'); // 'a' will be alerted
var dog = {
home: 'Doggy house',
bark: function() {
alert(this + ' is me.');
alert(this.home + ' is my home!');
}
};
dog.bark(); // [object Object] is me. Doggy house is my home.