[英]How can I parse or interpret the arguments given into console.log()
console.log(0..V); // [0, 1, 2, 3, 4];
console.log(0..VII); // [0, 1, 2, 3, 4, 5, 6];
I gotta implement such a behaviour for the console.log function.我必须为 console.log 函数实现这样的行为。 How can I interpret the arguments devided by ..
我如何解释由..
划分的论点
You could create a function which does something similar to this:您可以创建一个执行类似操作的函数:
const V = 5; const VII = 7; function getNumbers(from, to) { const result = []; for (let i = from; i < to; i++) { result.push(i); } return result; } console.log(getNumbers(0, V)); console.log(getNumbers(0, VII));
You could take a Generator
for Symbol.iterator
as prototype for Number
with a numerical value.您可以将Symbol.iterator
的Generator
作为具有数值的Number
原型。
Number.prototype[Symbol.iterator] = function* () { for (var i = 0; i < this; i++) { yield i; } }; console.log(...10);
` `
let arr = [1,2,3,4,5,6,7,8,9]
Number.prototype.__defineGetter__('X', ()=> {console.log(...arr)})
console.log(1..X)
` `
So far I came up with that implementation, thank you everyone for hel到目前为止,我想出了那个实现,谢谢大家的帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.