人生三从境界:昨夜西风凋碧树,独上高楼,望尽天涯路。 衣带渐宽终不悔,为伊消得人憔悴。 众里寻他千百度,蓦然回首,那人却在灯火阑珊处。

为已有类添加方法

多次调用函数f,传给它迭代数值,如要打印3次“hello”:

Number.prototype.times = function (f, context) {
	let n = this.valueOf();
	for (let i = 0; i < n; i++) f.call(context, i);
}
let n = 3;
n.times(i => {
	console.log(`hello ${i}`);
});

 

posted @ 2022-03-25 10:29  0a  阅读(32)  评论(0编辑  收藏  举报