extends 通过 extends 实现类的继承。
super()
1.子类 constructor 方法中必须有 super ,
且必须出现在 this 之前。
2.调用父类构造函数,只能出现在子类的constructor构造函数。
3.调用父类方法, super 作为对象,在普通方法中,指向父类的原型对象,
子类需要访问父类中的静态值,需要在子类的静态方法中访问,在静态方法中,指向父类
class Father {
constructor() {
this.a = 10;
}
sing(){
console.log(111111111);
}
}
class Child extends Father {
constructor() {
super();
}
}
let test = new Child();
console.log(test);
test.sing()
class Father {
constructor(){}
get a() {
return this._a;
}
}
class Child extends Father {
constructor(){
super();
}
set a(a) {
this._a = a;
}
}
let test = new Child();
test.a = 2;
console.log(test.a);
class Father1 {
constructor(){}
get a() {
return this._a;
}
set a(a) {
this._a = a;
}
}
class Child1 extends Father1 {
constructor(){
super();
}
}
let test1 = new Child1();
test1.a = 2;
console.log(test1.a);
class Father {
test(){
return 99;
}
static test1(){
return 100;
}
}
class Child2 extends Father {
constructor(){
super();
// 调用父类普通方法
console.log(super.test());
}
static test3(){
// 调用父类静态方法
return super.test1()+2;
}
}
console.log(Child2.test3());
let mytest = new Child2();
class Child extends Father {
constructor(){
super();
}
}
let mychild1 = new Child();
console.log(mychild1);
console.log(mychild1.test());
class Child1 extends Father {
constructor() {
super();
// console.log(super.test());
// console.log(this.test());
}
static test3(){
console.log(super.test1());
console.log(this.test1());
}
}
Child1.test3();
let mychild2 = new Child1();
console.log(mychild1.prototype==mychild2.prototype);
console.log(mychild1.__proto__==mychild2.__proto__);
console.log(mychild2.prototype.test());
console.log(mychild2.__proto__.test());
console.log(mychild2.test());
Object.setPrototypeOf() 静态方法可以将一个指定对象的原型
(即内部的 [[Prototype]] 属性)设置为另一个对象或者 null。
let obj1 = {
name:"zs"
}
let obj2 = {
age:20
}
Object.setPrototypeOf(obj1,obj2);
console.log(obj1);
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
</body>
<script type="text/javascript">
// extends 通过 extends 实现类的继承。
// super()
// 1.子类 constructor 方法中必须有 super ,
// 且必须出现在 this 之前。
// 2.调用父类构造函数,只能出现在子类的constructor构造函数。
// 3.调用父类方法, super 作为对象,在普通方法中,指向父类的原型对象,
// 子类需要访问父类中的静态值,需要在子类的静态方法中访问,在静态方法中,指向父类
// class Father {
// constructor() {
// this.a = 10;
// }
// sing(){
// console.log(111111111);
// }
// }
// class Child extends Father {
// constructor() {
// super();
// }
// }
// let test = new Child();
// console.log(test);
// test.sing()
// class Father {
// constructor(){}
// get a() {
// return this._a;
// }
// }
// class Child extends Father {
// constructor(){
// super();
// }
// set a(a) {
// this._a = a;
// }
// }
// let test = new Child();
// test.a = 2;
// console.log(test.a);
// class Father1 {
// constructor(){}
// get a() {
// return this._a;
// }
// set a(a) {
// this._a = a;
// }
// }
// class Child1 extends Father1 {
// constructor(){
// super();
// }
// }
// let test1 = new Child1();
// test1.a = 2;
// console.log(test1.a);
// class Father {
// test(){
// return 99;
// }
// static test1(){
// return 100;
// }
// }
// class Child2 extends Father {
// constructor(){
// super();
// // 调用父类普通方法
// console.log(super.test());
// }
// static test3(){
// // 调用父类静态方法
// return super.test1()+2;
// }
// }
// console.log(Child2.test3());
// let mytest = new Child2();
// class Child extends Father {
// constructor(){
// super();
// }
// }
// let mychild1 = new Child();
// console.log(mychild1);
// console.log(mychild1.test());
// class Child1 extends Father {
// constructor() {
// super();
// // console.log(super.test());
// // console.log(this.test());
// }
// static test3(){
// console.log(super.test1());
// console.log(this.test1());
// }
// }
// Child1.test3();
// let mychild2 = new Child1();
// console.log(mychild1.prototype==mychild2.prototype);
// console.log(mychild1.__proto__==mychild2.__proto__);
// console.log(mychild2.prototype.test());
// console.log(mychild2.__proto__.test());
// console.log(mychild2.test());
// Object.setPrototypeOf() 静态方法可以将一个指定对象的原型
// (即内部的 [[Prototype]] 属性)设置为另一个对象或者 null。
// let obj1 = {
// name:"zs"
// }
// let obj2 = {
// age:20
// }
// Object.setPrototypeOf(obj1,obj2);
// console.log(obj1);
var Father = {
name:"ls"
}
class Child{
constructor() {
}
}
Object.setPrototypeOf(Child.prototype,Father);
Child.prototype.ids = 99;
let aa = new Child();
console.log(aa);
console.log(aa.name);
</script>
</html>