笔记内容多摘录自《JavaScript设计模式与开发实践》(曾探著),侵删。
同一操作作用于不同的对象上,可以产生不同的解释和不同的执行结果。
多态背后的思想是将“做什么”和“谁去做”以及“怎么做”分离开来,也就是将“不变的事物”与“可能改变的事物分离开”
封装的目的是将信息隐藏
Object.create = Object.create || function( obj ){
var F = function(){};
F.prototype = obj;
return new F();
}
this的指向
用途
把请求封装为对象,分离请求发起者和接收者(执行者)之间的耦合关系
var Tv = {
open: function () {
console.log(‘打开电视机‘);
},
close: function () {
console.log(‘关上电视机‘);
}
};
var createCommand = function (receiver) {
var execute = function () {
return receiver.open(); // 执行命令,打开电视机
}
var undo = function () {
return receiver.close(); // 执行命令,关闭电视机
}
return {
execute: execute,
undo: undo
}
};
var setCommand = function (command) {
document.getElementById(‘execute‘).onclick = function () {
command.execute(); // 输出:打开电视机
}
document.getElementById(‘undo‘).onclick = function () {
command.undo(); // 输出:关闭电视机
}
};
setCommand(createCommand(Tv));
命令的接收着封闭在闭包中
函数柯里化、部分求值
var currying = function (fn) {
var args = [];
return function () {
if (arguments.length === 0) {
return fn.apply(this, args);
} else {
[].push.apply(args, arguments);
return arguments.callee;
}
}
};
var cost = (function () {
var money = 0;
return function () {
for (var i = 0, l = arguments.length; i < l; i++) {
money += arguments[i];
}
return money;
}
})();
var cost = currying(cost); // 转化成currying 函数
cost(100); // 未真正求值
cost(200); // 未真正求值
cost(300); // 未真正求值
alert(cost()); // 求值并输出:600
泛化this的提取过程
Function.prototype.uncurrying = function () {
var self = this;
return function () {
var obj = Array.prototype.shift.call(arguments);
return self.apply(obj, arguments);
};
};
Function.prototype.uncurrying = function () {
var self = this;
return function () {
return Function.prototype.call.apply(self, arguments);
}
};
var throttle = function (fn, interval) {
var __self = fn, // 保存需要被延迟执行的函数引用
timer, // 定时器
firstTime = true; // 是否是第一次调用
return function () {
var args = arguments,
__me = this;
if (firstTime) { // 如果是第一次调用,不需延迟执行
__self.apply(__me, args);
return firstTime = false;
}
if (timer) { // 如果定时器还在,说明前一次延迟执行还没有完成
return false;
timer = setTimeout(function () { // 延迟一段时间执行
clearTimeout(timer);
timer = null;
__self.apply(__me, args);
}, interval || 500);
};
}
};
var timeChunk = function (ary, fn, count) {
var obj,
t;
var len = ary.length;
var start = function () {
for (var i = 0; i < Math.min(count || 1, ary.length); i++) {
var obj = ary.shift();
fn(obj);
}
};
return function () {
t = setInterval(function () {
if (ary.length === 0) { // 如果全部节点都已经被创建好
return clearInterval(t);
}
start();
}, 200); // 分批执行的时间间隔,也可以用参数的形式传入
};
};
var ary = [];
for (var i = 1; i <= 1000; i++) {
ary.push(i);
};
var renderFriendList = timeChunk(ary, function (n) {
var div = document.createElement(‘div‘);
div.innerHTML = n;
document.body.appendChild(div);
}, 8);
renderFriendList();
var addEvent = function (elem, type, handler) {
if (window.addEventListener) {
addEvent = function (elem, type, handler) {
elem.addEventListener(type, handler, false);
}
} else if (window.attachEvent) {
addEvent = function (elem, type, handler) {
elem.attachEvent(‘on‘ + type, handler);
}
}
addEvent(elem, type, handler);
};
var div = document.getElementById(‘div1‘);
addEvent(div, ‘click‘, function () {
alert(1);
});
addEvent(div, ‘click‘, function () {
alert(2);
});
var debounce = function (method, delay) {
var timer = null;
return function () {
var _this = this;
var args = arguments;
clearTimeout(timer);
timer = setTimeout(function () {
method.apply(_this, args);
}, delay);
};
};
《JavaScript设计模式与开发实践》读书笔记-基础知识
原文:https://www.cnblogs.com/sunven/p/9446432.html