ES5中的继承
< script>
function Father ( uname, age ) {
this . uname = uname
this . age = age
}
function Son ( uname, age ) {
Father . call ( this , uname, age)
}
var son = new Son ( '大数据' , 12 )
console. log ( son)
< / script>
ES6中类的继承
class Parent {
name: string
static Adress: string = '成都市'
private uname: string = 'Tom'
#flag: boolean = false
constructor ( name: string ) {
this . name = name
}
getName ( ) {
return this . name
}
static getNames ( ) {
return this . name
}
}
class Child extends Parent {
age: number
constructor ( name: string , age: number ) {
super ( name)
this . age = age
}
}
const ch = new Child ( '李氏' , 18 )
console . log ( ch)
console . log ( Child. getNames ( ) )
console . log ( Object. getPrototypeOf ( ch) === Parent)
Object.getPrototypeOf
console . log ( Object. getPrototypeOf ( ch) === Parent)
super
作为函数
super()
作为对象
class Parent {
type: string
static getType : ( ) => string
constructor ( ) {
this . type = 'parent'
}
getName ( ) {
return this . type
}
}
Parent. getType = ( ) => {
return 'is Parent'
}
class Child extends Parent {
constructor ( ) {
super ( )
console . log ( 'constructor: ' + super . getName ( ) )
}
getParentName ( ) {
console . log ( 'getParentName: ' + super . getName ( ) )
}
static getParentType ( ) {
console . log ( 'getParentType:' + super . getType ( ) )
}
}
const ch = new Child ( )
ch. getParentName ( )
Child. getParentType ( )
类的prototype属性和_proto_属性
详见此处