-
Notifications
You must be signed in to change notification settings - Fork 0
/
demo10.js
95 lines (75 loc) · 1.67 KB
/
demo10.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/**
* Created by Administrator on 2017/8/28 0028.
*/
//适配器模式
//将一个类的借口(方法与属性)转化成另外一个接口,以满足用户需求,使类之间接口的不兼容问题通过适配器得以解决
//Jquery的适配
window.A = A = jQuery;
//适配异类框架
var A = A || {};
A.g = function(id){
return document.getElementById(id);
}
A,on = function(id,type,fn){
var dom = typeof id === 'string' ? this.g(id) :id
if(dom.addEventListener){
dom.addEventListener(type,fn,false)
}else if( dom.attachEvent ){
dom.attacheEvent('on' + type,fn)
}else{
dom['on'+type] = fn;
}
}
A.on(window,"load",function(){
a.on('myButton','click',function(){
//do something
})
})
A.g = function(id){
return $(id).get(0)
}
A.on = function(id,type,fn){
var dom = typeof id === 'string' ?$("#"+id) :$(id);
dom.on(type,fn)
}
//参数适配器
/*
* obj,name : name
*obj.title : title
* obj.age : age
* obj.size : size
* obj.prize : prize
*
*/
function doSomeThing(obj){}
function doSomeThing(obj){
var _adapter = {
name : "gaofeng",
title : "设计模式",
age : 100,
color :'pink',
size : "100",
prize : 50
}
for(var i in _adapter){
_adapter[i] = obj[i] || _adapter[i]
}
}
//数据适配
var arr = ['Javascript','book','前端编程语言'];
var obj = {
name : '',
type : '',
title: '',
time : ''
}
function arrrToObjectAdapter(arr){
return {
name : arr[0],
type : arr[1],
title : arr[2],
data : arr[3]
}
}
var adapterData = arrToObjectAdapter(arr);
console.log( adapterData )