JavaScript 2022

Term 3 Wednesday

May - July 2022

This site will be updated live during the sessions

Week 8 12 Apply 2 Slide Example Any Length Array

		
001window.onload = function(){	
		
002	const obj = {
		
003		age: 10
		
004	};
		
005	
		
006	//function that the apply will be on
		
007	// NOTE: we take the entire array as arr
		
008	const increaseAge = function(){
		
009		// we can use the arguments object to get array contents
		
010		//https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/arguments
		
011		for(let i = 0; i<arguments.length;i++)
		
012		{
		
013			this.age += arguments[i];
		
014			console.log('arr['+i+']: ' + arguments[i])
		
015			console.log('age: ' + this.age)
		
016			
		
017		}
		
018		return this.age;
		
019	}
		
020	
		
021	let arr = [1, 2, 3];
		
022	console.log('age before apply: '+ obj.age);
		
023	increaseAge.apply(obj , arr );
		
024	//obj.age will now have a value of 22
		
025	console.log('age after apply: '+ obj.age);
		
026	
		
027}