js implements A function and inherits B function

Prototype inheritance

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

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

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

new A().eat()

call and apply implement inheritance

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

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

new A().eat()

Guess you like

Origin blog.csdn.net/weixin_35958891/article/details/108436676