js implementa la función A y hereda la función B

Herencia de prototipos

function A(){
    
    
	this.name = '小明'
}

function B(name){
    
    
	this.eat = function(){
    
    
		console.log('调用b中的eat')
	}
}

// 实现a函数继承b函数
A.prototype = new B()		

new A().eat()

llamar y aplicar la herencia de implementación

function A(){
    
    
	B.call(this)
	this.name = '小明'
}

function B(){
    
    
	this.eat = function(){
    
    
		console.log('调用b中的eat')
	}
}		

new A().eat()

Supongo que te gusta

Origin blog.csdn.net/weixin_35958891/article/details/108436676
Recomendado
Clasificación