js對(duì)象的深度克隆
function clone(obj) {
var buf;
if (Obj instanceof Array) {
buf = []; //創(chuàng)建一個(gè)空的數(shù)組
var i = Obj.length;
while (i--) {
buf[i] = clone(Obj[i]);
}
return buf;
} else if (Obj instanceof Object) {
buf = {}; //創(chuàng)建一個(gè)空對(duì)象
for (var k in Obj) { //為這個(gè)對(duì)象添加新的屬性
buf[k] = clone(Obj[k]);
}
return buf;
} else {
return Obj;
}
}
js數(shù)組去重
// 以下是數(shù)組去重的三種方法:
Array.prototype.unique1 = function() {
var n = []; //一個(gè)新的臨時(shí)數(shù)組
for (var i = 0; i < this.length; i++) //遍歷當(dāng)前數(shù)組
{
//如果當(dāng)前數(shù)組的第i已經(jīng)保存進(jìn)了臨時(shí)數(shù)組,那么跳過(guò),
//否則把當(dāng)前項(xiàng)push到臨時(shí)數(shù)組里面
if (n.indexOf(this[i]) == -1) n.push(this[i]);
}
return n;
}
Array.prototype.unique2 = function()
{
var n = {},
r = []; //n為hash表,r為臨時(shí)數(shù)組
for (var i = 0; i < this.length; i++) { //遍歷當(dāng)前數(shù)組
if (!n[this[i]]) { //如果hash表中沒(méi)有當(dāng)前項(xiàng)
n[this[i]] = true; //存入hash表
r.push(this[i]); //把當(dāng)前數(shù)組的當(dāng)前項(xiàng)push到臨時(shí)數(shù)組里面
}
}
return r;
}
Array.prototype.unique3 = function(){
var n = [this[0]]; //結(jié)果數(shù)組
for (var i = 1; i < this.length; i++) //從第二項(xiàng)開(kāi)始遍歷
{
//如果當(dāng)前數(shù)組的第i項(xiàng)在當(dāng)前數(shù)組中第一次出現(xiàn)的位置不是i,
//那么表示第i項(xiàng)是重復(fù)的,忽略掉。否則存入結(jié)果數(shù)組
if (this.indexOf(this[i]) == i) n.push(this[i]);
}
return n;
}
js常用設(shè)計(jì)模式的實(shí)現(xiàn)思路,單例,工廠,代理,裝飾,觀察者模式等
// 1) 單例:任意對(duì)象都是單例,無(wú)須特別處理
var obj = {name: 'michaelqin', age: 30};
// 2) 工廠: 就是同樣形式參數(shù)返回不同的實(shí)例
function Person() { this.name = 'Person1'; }
function Animal() { this.name = 'Animal1'; }
function Factory() {}
Factory.prototype.getInstance = function(className) {
return eval('new ' + className + '()');
}
var factory = new Factory();
var obj1 = factory.getInstance('Person');
var obj2 = factory.getInstance('Animal');
console.log(obj1.name); // Person1
console.log(obj2.name); // Animal1
//3) 代理: 就是新建個(gè)類調(diào)用老類的接口,包一下
function Person() { }
Person.prototype.sayName = function() { console.log('michaelqin'); }
Person.prototype.sayAge = function() { console.log(30); }
function PersonProxy() {
this.person = new Person();
var that = this;
this.callMethod = function(functionName) {
console.log('before proxy:', functionName);
that.person[functionName](); // 代理
console.log('after proxy:', functionName);
}
}
var pp = new PersonProxy();
pp.callMethod('sayName'); // 代理調(diào)用Person的方法sayName()
pp.callMethod('sayAge'); // 代理調(diào)用Person的方法sayAge()
//4) 觀察者: 就是事件模式,比如按鈕的onclick這樣的應(yīng)用.
function Publisher() {
this.listeners = [];
}
Publisher.prototype = {
'addListener': function(listener) {
this.listeners.push(listener);
},
'removeListener': function(listener) {
delete this.listeners[listener];
},
'notify': function(obj) {
for(var i = 0; i < this.listeners.length; i++) {
var listener = this.listeners[i];
if (typeof listener !== 'undefined') {
listener.process(obj);
}
}
}
}; // 發(fā)布者
function Subscriber() {
}
Subscriber.prototype = {
'process': function(obj) {
console.log(obj);
}
}; // 訂閱者
var publisher = new Publisher();
publisher.addListener(new Subscriber());
publisher.addListener(new Subscriber());
publisher.notify({name: 'michaelqin', ageo: 30}); // 發(fā)布一個(gè)對(duì)象到所有訂閱者
publisher.notify('2 subscribers will both perform process'); // 發(fā)布一個(gè)字符串到所有訂閱者