forked from huiyan-fe/mapv
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.js
More file actions
110 lines (102 loc) · 3.7 KB
/
Copy pathutil.js
File metadata and controls
110 lines (102 loc) · 3.7 KB
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/**
* @author nikai (@胖嘟嘟的骨头, nikai@baidu.com)
* 一些常用的方法库
*/
var util = {
isPlainObject: function (obj) {
var key;
var class2type = {};
var hasOwn = class2type.hasOwnProperty;
// Must be an Object.
// Because of IE, we also have to check the presence of the constructor property.
// Make sure that DOM nodes and window objects don't pass through, as well
if (!obj || typeof obj !== 'object' || obj.nodeType) {
return false;
}
// Not own constructor property must be Object
var hasNoOwn = !hasOwn.call(obj, 'constructor');
var hasNoOwnPrototypeOf = !hasOwn.call(obj.constructor.prototype, 'isPrototypeOf');
if (obj.constructor && hasNoOwn && hasNoOwnPrototypeOf) {
return false;
}
// Own properties are enumerated firstly, so to speed up,
// if last one is own, then all properties are own.
for (key in obj) {}
return key === undefined || hasOwn.call(obj, key);
},
/**
* 深度扩展一个对象
*/
extend: function (destination, source) {
var i,
toStr = Object.prototype.toString,
astr = '[object Array]';
destination = destination || {};
for (i in source) {
if (source.hasOwnProperty(i)) {
if (util.isPlainObject(source[i])) {
destination[i] = (toStr.call(source[i]) === astr) ? [] : {};
arguments.callee(destination[i], source[i]);
destination[i] = source[i];
} else {
destination[i] = source[i];
}
}
}
return destination;
},
/**
* copy an object
* @param {Object} obj the obj
* @return {Object} new object
*/
copy: function (obj) {
return this.extend({}, obj);
},
/**
* 为类型构造器建立继承关系
* @name baidu.lang.inherits
* @function
* @grammar baidu.lang.inherits(subClass, superClass[, className])
* @param {Function} subClass 子类构造器
* @param {Function} superClass 父类构造器
* @remark
*
使subClass继承superClass的prototype,因此subClass的实例能够使用superClass的prototype中定义的所有属性和方法。<br>
这个函数实际上是建立了subClass和superClass的原型链集成,并对subClass进行了constructor修正。<br>
<strong>注意:如果要继承构造函数,需要在subClass里面call一下,具体见下面的demo例子</strong>
* @shortcut inherits
* @meta standard
*/
inherits: function (subClass, superClass) {
var key;
var proto;
var selfProps = subClass.prototype;
var Clazz = new Function();
Clazz.prototype = superClass.prototype;
proto = subClass.prototype = new Clazz();
for (key in selfProps) {
proto[key] = selfProps[key];
}
subClass.prototype.constructor = subClass;
subClass.superClass = superClass.prototype;
},
// 在页面中添加样式
addCssByStyle: function (cssString) {
var doc = document;
var style = doc.createElement('style');
style.setAttribute('type', 'text/css');
if (style.styleSheet) { // IE
style.styleSheet.cssText = cssString;
} else { // w3c
var cssText = doc.createTextNode(cssString);
style.appendChild(cssText);
}
var heads = doc.getElementsByTagName('head');
if (heads.length) {
heads[0].appendChild(style);
} else {
doc.documentElement.appendChild(style);
}
}
}