JavaScript factory functions and Object.create()

Do you know JavaScript factory function and its issues, and why we use Object.create() method instead?

Hello 🖐,

Factory functions in JS are any function that return an object.

like this one:

function createPerson(firstName, lastName) {
retur…

Do you know JavaScript factory function and its issues, and why we use Object.create() method instead?

Hello 🖐,

Factory functions in JS are any function that return an object.

like this one:

function createPerson(firstName, lastName) {
    return {
        firstName: firstName,
        lastName: lastName,
        getFullName() {
            return firstName + ' ' + lastName;
        }
    }
}
const person1 = createPerson("Ahmad", "Mukahal");
const person1 = createPerson("john", "Deo");

and so on…

With the factory function, you create any number of the person objects you want without duplicating code.

What if we have one thousand persons? It will store a thousand objects in the memory heap! and this is not an efficient way.

Example:
Each object will have a different address in the memory and every object will have the getFullName() method, Ooh no, I’m not DRY!!

This is why the Object.create() method comes into play.

The Object.create() method creates a new object using an existing object as the prototype of the new object:

const person = {
firstName : "John",
lastName: "Deo",
getFullName() {
            return firstName + ' ' + lastName;
        }
}

// Make prototype chain:

const me = Object.create(person);

so, now me object has all properties and methods in person object, and it can hold its own props and methods and override props and methods as you want like this:

console.log(me.firstName) // John
me.firstName = "Ahmad";
me.lastName = "Mukahal";
console.log(me.firstName) // Ahmad

What efficient is that !!

Hopefully enjoyed reading.

Ahmad Mukahal 🌹


Print Share Comment Cite Upload Translate
APA
Ahmad Mkahal | Sciencx (2024-03-29T14:59:36+00:00) » JavaScript factory functions and Object.create(). Retrieved from https://www.scien.cx/2022/01/14/javascript-factory-functions-and-object-create/.
MLA
" » JavaScript factory functions and Object.create()." Ahmad Mkahal | Sciencx - Friday January 14, 2022, https://www.scien.cx/2022/01/14/javascript-factory-functions-and-object-create/
HARVARD
Ahmad Mkahal | Sciencx Friday January 14, 2022 » JavaScript factory functions and Object.create()., viewed 2024-03-29T14:59:36+00:00,<https://www.scien.cx/2022/01/14/javascript-factory-functions-and-object-create/>
VANCOUVER
Ahmad Mkahal | Sciencx - » JavaScript factory functions and Object.create(). [Internet]. [Accessed 2024-03-29T14:59:36+00:00]. Available from: https://www.scien.cx/2022/01/14/javascript-factory-functions-and-object-create/
CHICAGO
" » JavaScript factory functions and Object.create()." Ahmad Mkahal | Sciencx - Accessed 2024-03-29T14:59:36+00:00. https://www.scien.cx/2022/01/14/javascript-factory-functions-and-object-create/
IEEE
" » JavaScript factory functions and Object.create()." Ahmad Mkahal | Sciencx [Online]. Available: https://www.scien.cx/2022/01/14/javascript-factory-functions-and-object-create/. [Accessed: 2024-03-29T14:59:36+00:00]
rf:citation
» JavaScript factory functions and Object.create() | Ahmad Mkahal | Sciencx | https://www.scien.cx/2022/01/14/javascript-factory-functions-and-object-create/ | 2024-03-29T14:59:36+00:00
https://github.com/addpipe/simple-recorderjs-demo