写JS继承
//组合寄生
function inheritProtoType(parent,child){
const protoType = Object.create(parent.prototype);
protoType.constructor = child;
child.prototype = protoType;
}
function Parent(name){
this.name = name;
this.color = ['red'];
}
Parent.prototype.getName = function (){
return this.name;
}
function Child(name,age){
Parent.call(this,name);
this.age = age;
}
inheritProtoType(Parent,Child);
var instance = new Child('jack',20);
console.log(instance.getName());
//ES6继承
class Person{
constructor(name) {
this.name = name;
}
getName = function() {
console.log('Person',this.name);
}
}
class Game extends Person{
constructor(name,age) {
super(name);
this.age = age;
}
}
<!DOCTYPE html>
<html>
<head>
<title>test</title>
</head>
<body>
<header></header>
<article>
<section>
<p>
<small></small>
</p>
</section>
<section>
<div>
<a></a>
</div>
</section>
<section>
<ul>
<li></li>
<li></li>
</ul>
</section>
</article>
<script>
/**
* 实现 trav 函数,按下列顺序输出所有元素节点名称
* 元素节点 nodeType: 1,获取元素名称使用 nodeName
* BODY
* HEADER
* ARTICLE
* SECTION
* P
* SMALL
* SECTION
* DIV
* A
* SECTION
* UL
* LI
* LI
* SCRIPT
*/
function trav(ele) {
// ele.firstChild,
// ele.nextSibling,
// ele.nodeType,
// ele.nodeName
if(ele.nodeType === 1){
console.log(ele.nodeName);
}
for(let child = ele.firstChild; child != null;child = child.nextSibling){
trav(child);
}
}
trav(document.body);
</script>
</body>
</html>
Comments NOTHING