Object with anonymous functions in JavaScript

In the previous lesson, we made an array consisting of functions. Let's now instead of an array make an object with keys whose values will be functions:

let obj = { func1: function() {console.log(1)}, func2: function() {console.log(2)}, func3: function() {console.log(3)}, };

Let's use our object:

let obj = { func1: function() {console.log(1)}, func2: function() {console.log(2)}, func3: function() {console.log(3)}, }; obj.func1(); // shows 1

Make an object with three functions. Let the first return through return the number 1, the second - the number 2, the third - the number 3. Using the created functions, print the sum of the returned numbers to the console.

Loop through the created object and output the results of the functions work to the console.

Application

Let's make an object with two functions, each of them will take a number as a parameter. Let the first function square the passed number, and the second function cube. An implementation:

let math = { square: function(num) {return num * num}, cube: function(num) {return num * num * num}, };

Let's use our functions:

let math = { square: function(num) {return num * num}, cube: function(num) {return num * num * num}, }; console.log( math.square(2) ); // shows 4 console.log( math.cube(2) ); // shows 8

Make an object with three functions, each of them will take an array of numbers as a parameter. Make the first function return the sum of the array elements, the second function return the sum of squares, and the third return the sum of cubes.

enru