Logo

dev-resources.site

for different kinds of informations.

Factory functions with private variables in JavaScript

Published at
6/23/2024
Categories
webdev
javascript
functional
factory
Author
anoopaneesh
Author
11 person written this
anoopaneesh
open
Factory functions with private variables in JavaScript

We all know classes in JavaScript , it is a template for creating objects.Classes are created using the "class" keyword in JS. But before the introduction of class we had an alternate way of achieving this object oriented approach - Factory Functions

Factory functions are simple javascript functions which returns an object

Factory function example

Note that we don't require a "new" keyword to create the object.

The "this" keyword used inside the function will refer to the execution context (environment in which the function is executed) of that function.

Now we have private variables in classes , Let us see how we can achieve this using factory functions.

We can make use of Closures in JavaScript. What is a Closure?
A closure is the collection of variables and functions that is being referenced.

The variables used inside a function cannot be accessed outside unless it returns the variable.

We can make use of these to simulate private variables using factory functions

Private variables using factory function

Here the variables id and marks cannot be used outside the function since it is block scoped. But it can be accessed by the inner function (status) , because it forms a closure with those variables.

References
Classes : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes
Closures : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Closures
Execution Context : https://www.freecodecamp.org/news/how-javascript-works-behind-the-scene-javascript-execution-context/

factory Article's
25 articles in total
Favicon
Clojure is Awesome!!!
Favicon
Understanding the Factory and Factory Method Design Patterns
Favicon
Factory Design Pattern
Favicon
Things You Want to Ensure When Factory Resetting Your HP Laptop Without a Password
Favicon
The Factory Pattern in C#: Creating Objects the Smart Way
Favicon
Enhance Your Factory Car Radio's Bass with Affordable Upgrades
Favicon
Page Object Model and Page Factory in Selenium
Favicon
Factory functions with private variables in JavaScript
Favicon
Evoluindo nosso Projeto Rails: Integrando o Padrão Factory para Maior Flexibilidade e Organização
Favicon
Javascript Factory Design Pattern
Favicon
Dart Abstract and Factory Keywords
Favicon
Azure Data Factory Overview For Beginners
Favicon
Improve your factories in Symfony
Favicon
Reduzindo a quantidade de Branchs na criação de Objetos com uma estrutura plugável
Favicon
Java 9 Factory Method for Collections: List, Set, Map
Favicon
How to implement simple Factory Pattern in Node.js
Favicon
Factory Design Pattern (simple example implementation in PHP)
Favicon
Spring's FactoryBean Interface
Favicon
Custom Validators for Angular Reactive Forms
Favicon
Design Patterns: Factory
Favicon
Creating objects dynamically with factory pattern in javascript
Favicon
Is this the real life, is this just fantasy?
Favicon
Design Patterns: Factory Pattern, Part 2
Favicon
Patterns in Kotlin: Abstract Factory
Favicon
SUT Factory

Featured ones: