命名空间
命名空间
命名空间的意义
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>JS Bin</title>
</head>
<body>
<ul>
<li id="item1">选项1</li>
<li id="item2">选项2</li>
<li id="item3">选项3</li>
<li id="item4">选项4</li>
<li id="item5">选项5</li>
</ul>
</body>
</html>
--------------------------------------------------------------------
/*命名空间*/
window.aWang = {}
/*添加一个获取兄弟们的方法*/
aWang.getSiblings = function(node){ /*API*/
var allChildren = node.parentNode.children
var array = {
length:0
}
for(let i = 0 ;i<allChildren.length; i++){
if(allChildren[i] !== node){
array[array.length] = allChildren[i]
array.length += 1
}
}
return array
}
/*添加一个批量添加class的方法*/
aWang.addClass = function(node,classes){
for(let key in classes){
var value = classes[key]
var methodName = value ? 'add' : 'remove'
node.classList[methodName](key)
}
}
console.log(aWang.getSiblings(item3))
aWang.addClass(item3,{'a':true,'b':true,'c':true})
网友评论