简体   繁体   中英

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. 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.

 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM