How to use a Function object in JavaScript

7 Answers

0 votes
/*
In JavaScript, functions are first-class objects, meaning they can be assigned to variables, 
stored in objects, passed as arguments, returned from other functions, and have their 
own properties and methods.
*/

/*
   Best Practices:
✓ Use descriptive function names.
✓ Prefer const for function references.
✓ Keep functions focused on a single responsibility.
✓ Use closures when you need private state.
✓ Add properties to functions only when it improves clarity.
✓ Prefer object methods or classes for complex stateful behavior.
✓ Use higher-order functions to create reusable abstractions.
*/

/*
=====================================================
TITLE: Basic Function Object Example
=====================================================

A function in JavaScript is also an object.
It can be assigned to variables and invoked later.
*/

function greet(name) {
    return `Hello, ${name}!`;
}

// Store the function in another variable.
const sayHello = greet;

// Invoke the function through the variable.
console.log(sayHello("Kyron"));

// Functions are objects and have properties.
console.log("Function name:", greet.name);
console.log("Parameter count:", greet.length);



/*
run:

Hello, Kyron!
Function name: greet
Parameter count: 1

*/

 



answered Oct 10 by avibootz
edited Oct 10 by avibootz
0 votes
/*
In JavaScript, functions are first-class objects, meaning they can be assigned to variables, 
stored in objects, passed as arguments, returned from other functions, and have their 
own properties and methods.
*/

// Adding Properties to a Function Object

/*
=====================================================
TITLE: Function Object with Custom Properties
=====================================================

Functions can hold custom properties just like
ordinary objects.
*/

function calculateSquare(number) {
    return number * number;
}

// Add custom metadata.
calculateSquare.description = "Calculates the square of a number";
calculateSquare.version = "1.0";

console.log(calculateSquare(5));

console.log("Description:", calculateSquare.description);
console.log("Version:", calculateSquare.version);



/*
run:

25
Description: Calculates the square of a number
Version: 1.0

*/

 



answered Oct 10 by avibootz
0 votes
/*
In JavaScript, functions are first-class objects, meaning they can be assigned to variables, 
stored in objects, passed as arguments, returned from other functions, and have their 
own properties and methods.
*/

/*
 * ============================================
 * TITLE: Passing Function Objects as Arguments
 * ============================================
 *
 * Functions can be passed to other functions,
 * which enables flexible and reusable code.
 */

function performOperation(a, b, operation) {
    return operation(a, b);
}

function add(x, y) {
    return x + y;
}

function multiply(x, y) {
    return x * y;
}

console.log(performOperation(10, 5, add));
console.log(performOperation(10, 5, multiply));


/*
run:

15
50

*/

 



answered Oct 10 by avibootz
0 votes
/*
In JavaScript, functions are first-class objects, meaning they can be assigned to variables, 
stored in objects, passed as arguments, returned from other functions, and have their 
own properties and methods.
*/

/*
====================================================
TITLE: Returning Function Objects
====================================================

Functions can create and return other functions.
This technique is commonly used in closures.
*/

function createMultiplier(factor) {
    return function (value) {
        return value * factor;
    };
}

const double = createMultiplier(2);
const triple = createMultiplier(3);

console.log(double(10));
console.log(triple(10));



/*
run:

20
30

*/

 



answered Oct 10 by avibootz
0 votes
/*
In JavaScript, functions are first-class objects, meaning they can be assigned to variables, 
stored in objects, passed as arguments, returned from other functions, and have their 
own properties and methods.
*/

// Function Object Inside an Object -  Used as a method

/*
===================================================
TITLE: Using Function Objects as Object Methods
===================================================

Functions can be attached to objects and used
as methods.
*/

const bankAccount = {
    owner: "Alice",
    balance: 1000,

    deposit(amount) {
        this.balance += amount;
      
        return this.balance;
    },

    withdraw(amount) {
        this.balance -= amount;
      
        return this.balance;
    }
};

console.log(bankAccount.deposit(500));
console.log(bankAccount.withdraw(250));
console.log(bankAccount.balance);


/*
run:

500
1250
1250

*/

 



answered Oct 10 by avibootz
0 votes
/*
In JavaScript, functions are first-class objects, meaning they can be assigned to variables, 
stored in objects, passed as arguments, returned from other functions, and have their 
own properties and methods.
*/

// Callable Function Object with Metadata

/*
=====================================================
TITLE: Advanced Function Object Pattern
=====================================================

This example demonstrates a function object that
stores metadata about itself.

This pattern can be useful for logging, plugins,
middleware, and command systems.
*/

function createCommand(name, handler) {
    function command(...args) {
        console.log(`Executing command: ${command.commandName}`);
        return handler(...args);
    }

    command.commandName = name;
    command.createdAt = new Date();

    return command;
}

const greetCommand = createCommand(
    "greet",
    (userName) => `Welcome, ${userName}!`
);

console.log(greetCommand("Luna"));
console.log("Name:", greetCommand.commandName);
console.log("Created:", greetCommand.createdAt);


/*
run:

Executing command: greet
Welcome, Luna!
Name: greet
Created: 2026-10-10T10:14:32.139Z

*/

 



answered Oct 10 by avibootz
0 votes
/*
In JavaScript, functions are first-class objects, meaning they can be assigned to variables, 
stored in objects, passed as arguments, returned from other functions, and have their 
own properties and methods.
*/

// Using Function Objects in Collections

/*
/*
=====================================================
TITLE: Storing Function Objects in Arrays
=====================================================

Function objects can be stored in arrays and
executed dynamically.
*/

const operations = [
    (a, b) => a + b,
    (a, b) => a - b,
    (a, b) => a * b,
    (a, b) => a / b
];

const x = 20;
const y = 10;

for (const operation of operations) {
    console.log(operation(x, y));
}


/*
run:

30
10
200
2

*/

 



answered Oct 10 by avibootz

Related questions

...