/*修改Node的原型链*/
<!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>
------------------------------------------------------------
/*修改Node的原型链*/
Node.prototype.getSiblings = function(){
var allChildren = this.parentNode.children
var array = {
length:0
}
for(let i = 0 ;i<allChildren.length; i++){
if(allChildren[i] !== this){
array[array.length] = allChildren[i]
array.length += 1
}
}
return array
}
Node.prototype.addClass = function(classes){
for(let key in classes){
var value = classes[key]
var methodName = value ? 'add' : 'remove'
this.classList[methodName](key)
}
}
console.dir(item3)
console.log(item3)
console.log(item3.getSiblings())
console.log(item3.addClass({'a':true,'b':true,'c':true}))
console.log(item3.getSiblings.call(item3))
console.log(item3.addClass.call(item3,{'a':true,'b':true,'c':true}))
------------------------------------------------------------------------------
-----------------------Node2[无侵入]---------------------------------------
<!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.Node2 = function(node){
return{
getSiblings:function(){
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
},
addClass:function(classes){
for(let key in classes){
var value = classes[key]
var methodName = value ? 'add' : 'remove'
node.classList[methodName](key)
}
}
}
}
var node2 = Node2(item3)
console.log(Node2)
console.log(node2)
console.log(node2.getSiblings())
node2.addClass({'a':true,'b':true,'c':true})
console.log(item3)
修改原型链/Node2无侵入
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 本篇博客为"高性能JavaScript"一书部分知识点学习笔记摘录. 作用域链及和标识符解析 每一个JavaScr...
- 普通对象和函数对象 函数对象:使用函数声明、函数表达式、Function构造函数创建的对象 函数实际上是对象,每个...