面向对象,类的实例、继承
类与实例
- 类的声明
function A(){
this.name = "name"
}
class A2{
constructor(){
this.name="name"
}
}
- 生成实例
new A(),new A2()
类的继承
- 如何继承
function P(){
this.name = 'P'
}
P.prototype.say = function(){}
function C(){
P.call(this)
this.type = 'C'
}
function P(){
this.name = 'P'
this.play = [1,2,3]
}
function C(){
this.type = 'C'
}
C.prototype = new P()
var s1 = new C()
var s2 = new C()
s1.play.push(4)
function P(){
this.name = 'P'
this.play = [1,2,3]
}
function C(){
P.call(this)
this.type = 'C'
}
C.prototype = new P()
var s1 = new C()
var s2 = new C()
s1.play.push(4)
function P(){
this.name = 'P'
this.play = [1,2,3]
}
function C(){
P.call(this)
this.type = 'C'
}
C.prototype = P.prototype
var s1 = new C()
var s2 = new C()
s1.play.push(4)
console.log(s2.constructor)
function P(){
this.name = 'P'
this.play = [1,2,3]
}
function C(){
P.call(this)
this.type = 'C'
}
C.prototype = Object.create(P.prototype)
C.prototype.constructor = C
var s1 = new C()
var s2 = new C()
s1.play.push(4)
console.log(s2.constructor)
- 继承方式