Q
How can you store a function as a property of an object?

Answer & Solution

Answer: Option A
Solution:
A function can be stored as a property of an object using var obj = { method: function() { alert('Hello'); } };.
Related Questions on Average

What is the output of the following code? function makeAdder(a) { return function(b) { return a + b; }; } var add5 = makeAdder(5); console.log(add5(2));

A). 7

B). 5

C). add5

D). makeAdder

Which of the following is true about functions in JavaScript?

A). Functions can be assigned to variables

B). Functions can be passed as arguments to other functions

C). Functions can be returned from other functions

D). All of the above

Which of the following is an example of an arrow function assigned to a variable?

A). var add = (a, b) => { return a + b; };

B). var add = (a, b) => return a + b;

C). var add = (a, b) => { a + b };

D). var add = (a, b) { return a + b; };

How can you define a method inside an object?

A). var obj = { method: function() { alert('Hello'); } };

B). var obj = { method() { alert('Hello'); } };

C). Both A and B

D). var obj = { function method() { alert('Hello'); } };

Which of the following correctly demonstrates a function returning another function?

A). function outer() { return function() { alert('Inner'); }; }

B). function outer() { function() { alert('Inner'); } return; }

C). function outer() { function() { alert('Inner'); }; }

D). function outer() { return function { alert('Inner'); }; }

What is the term for a function that is passed as an argument to another function?

A). Callback function

B). Nested function

C). Helper function

D). Inner function

How do you define an anonymous function assigned to a variable?

A). var anon = function() { alert('Anonymous!'); };

B). var anon = function { alert('Anonymous!'); };

C). function anon() { alert('Anonymous!'); }

D). var anon = function anonymous() { alert('Anonymous!'); };

How do you assign a function to a variable in JavaScript?

A). var greet = function() { alert('Hello'); };

B). var greet = function { alert('Hello'); };

C). function greet() { alert('Hello'); }

D). var greet = function alert('Hello');

How do you create an Immediately Invoked Function Expression (IIFE)?

A). (function() { alert('IIFE'); })();

B). function() { alert('IIFE'); }();

C). (function { alert('IIFE'); })();

D). function() { alert('IIFE'); };

What is the output of the following code? var person = { name: 'Alice', greet: function() { return 'Hello, ' + this.name; } }; console.log(person.greet());

A). Hello, Alice

B). Hello, undefined

C). Hello, name

D). undefined