面试一脸懵:add(1,2).add(3).add(4).output()

实现以下的add()的方法
output()时打印前面的参数之和

add(1,2).add(3).add(4).output()

function add(...args){
    
    
	return [...args]
}
Array.prototype.add = function(...args){
    
    
	this.push(...args)
	return this
}
Array.prototype.output = function(){
    
    
	return this.reduce((a,b)=>a+b,0)
}

补充:

function add(){
    
    
	arguments.callee.add = arguments.callee.bind(arguments.callee, ...arguments)
	const args = [...arguments]
	arguments.callee.output = function(){
    
    
		return args.reduce((x, y)=> x+y,0)
	}
	return arguments.callee
}

猜你喜欢

转载自blog.csdn.net/m0_37285193/article/details/121703967
Add