59 lines
		
	
	
		
			912 B
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
			
		
		
	
	
			59 lines
		
	
	
		
			912 B
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
| let range = {
 | |
| 				from: 1,
 | |
| 				to: 5
 | |
| }
 | |
| 
 | |
| range[Symbol.iterator] = function(){
 | |
| 
 | |
| 	return {
 | |
| 		current: this.from,
 | |
| 		last: this.to,
 | |
| 		
 | |
| 		next(){
 | |
| 			if(this.current <= this.last){
 | |
| 				return {done: false, value: this.current++};
 | |
| 			} else {
 | |
| 				return {done: true};
 | |
| 			}
 | |
| 		}
 | |
| 	}
 | |
| };
 | |
| 
 | |
| 
 | |
| for(let num of range){
 | |
| 	console.log(num);
 | |
| }
 | |
| 
 | |
| for(let ch of "tests"){
 | |
| 	console.log(ch)
 | |
| }
 | |
| 
 | |
| let arrayLike = {
 | |
| 	0: "Hello",
 | |
| 	1: "World",
 | |
| 	length: 2
 | |
| };
 | |
| 
 | |
| // Array.from
 | |
| let arr = Array.from(arrayLike);
 | |
| console.log(arr.pop()); // World
 | |
| arr = Array.from(range);
 | |
| console.log(arr); //1, 2, 3, 4, 5
 | |
| arr = Array.from(range, num => num * num);
 | |
| console.log(arr); //1, 4, 9, 16, 25
 | |
| 
 | |
| let s = "split";
 | |
| let split_s = s.split('');
 | |
| let arr_s = Array.from(s);
 | |
| console.log(split_s);
 | |
| console.log(split_s[0]);
 | |
| console.log(split_s[1]);
 | |
| console.log(split_s.length);
 | |
| console.log(arr_s);
 | |
| console.log(arr_s[0]);
 | |
| console.log(arr_s[1]);
 | |
| console.log(arr_s.length);
 | |
| 
 | |
| 
 | |
| 
 |