Video Title: JavaScript Factory Functions
In the last lecture you learn how to use the object literal syntax to create an object but there is a tiny problem here imagine you want to create two circle objects so I’m gonna copy this code here and call this second circle object circle – now the problem we have here is
That we have duplicated the implementation of the draw method so right now it’s a simple method it has only a single line of code but what if we had ten lines of code here we wouldn’t want to repeat all these lines here because if you have a bug in this
Method then we’ll have to fix it in multiple places and also take into account that our circle object currently has only a single method what if we had ten other methods here we don’t want to duplicate or repeat all that logic right so if our objects have logic we need a
Different way to create objects that’s when we use factoring or constructor functions in this lecture you’re going to learn about Factory functions and we’ll look at constructor functions next so I’m gonna delete this second circle and show you how to create a factory function so factory function just like a
Factory producing products these factory functions produce objects so we define a function and call it create circle now we’re going to move this definition of the circle object inside of our factory function so we have a circle object here finally we need to return this now one
Way is to return it like this but we don’t really need this circle constant defined because we are not going to reference it anywhere we only want to return it so we can make this code shorter by removing the circle constant and simply returning this object so whenever we call the create circle
Function we’ll get a circle object however we have hard-coded these values here so every circle that is created by this function it’s radius will be one that’s not what you want maybe we want to have a circle with a bigger radius so we want to pass
Radius as a parameter here so radius and then instead of hard-coding one you want to set the value so this radius argument that we supply when calling this function similarly we can add another parameter here like location and instead of hard-coding this object here we can simply set location to this location
Argument however for simplicity I’m gonna remove these two properties location and is visible so we can focus on the core of factory functions so let’s delete these properties we don’t need the location parameter either now we can make this code a little bit shorter so in modern JavaScript if our
Key and value are the same we can make our code shorter by removing the value and simply adding the key so that is exactly equivalent to this code right so that is better next we have our draw method there is also a shorter syntax to define this method so instead of
Defining it as a key value pair we can define it like this so draw parentheses and code block this is similar to how we define a function outside of an object right so if you want to define a function this is how we define it right
So we have the function keyword then the name of the function parentheses and code block now when we put this inside of an object basically we’re dropping the function keyword and simply adding this inside of the object you can see this syntax is a little bit shorter and what
We have here so let me delete this and move this console not log statement in our draw method so now we have a factory function we can simply call this to create a circle object so I’m going to define this constant circle one and call the create
Circle function and pass one as the radius now let’s log this circle object on the console circle one save the changes so you can see radius is set to one we also have this draw method so we can call circle one but draw and here’s our draw message on the console now the
Beauty of this Factory function is that we have defined our logic in one place so we can call this function with different values or different arguments we get different circle objects but we have defined the draw method only in one place so if there is a bug in this
Method that we need to fix in the future there is a single place that we need to modify so now I’m gonna create another circle object circle to create circle I’m gonna pass to as the radius let’s log this on the console circle to save the changes look we have two different
Circle objects and a single definition of the draw method so this is all about factory functions but factory functions are not the only way to create objects we can also use constructor functions and that’s what you’re going to learn next hi guys thank you for watching my javascript tutorial this tutorial is
Part of my JavaScript course where you learn all the essential JavaScript features that every web and mobile application developer must know if you’re an absolute beginner or have some experience in JavaScript and are looking for a fun and in-depth course that teaches you the fundamentals of JavaScript this course is for you this
Course is also packed with tons of exercises that help you master what you learned in the course in fact many of these exercises are questions that come up in technical programming interviews so if you’re pursuing a job as a front-end or a back-end developer or if
You simply want to have a more in-depth understanding of JavaScript I highly encourage you to enroll in the course for a limited time you can get this course with a discount using the link in the video description click the link to find out more about the course and enroll
-
Sale!
Wireless WIFI Repeater Extender Amplifier Booster 300Mbps
$29.99$14.99 Add to cartWireless WIFI Repeater Extender Amplifier Booster 300Mbps
Categories: Electronics, Wi-Fi Router, Wireless Wi-Fi Extender Tags: 300Mbps, 802.11N, Amplifier, Booster, Extender, mobile wi-fi booster, Remote, WIFI, Wireless, Wireless WIFI, Wireless WIFI Repeater, Wireless WIFI Repeater Extender, Wireless WIFI Repeater Extender Amplifier, Wireless WIFI Repeater Extender Amplifier Booster, Wireless WIFI Repeater Extender Amplifier Booster 300Mbps$29.99$14.99 -
Sale!
Full RGB Light Design Gaming Headset Headphones with Mic
$24.99$14.99 Add to cartFull RGB Light Design Gaming Headset Headphones with Mic
Categories: Electronics, Gaming, Gaming Headsets Tags: Design, Full, Full RGB Light Design Gaming Headset, Full RGB Light Design Gaming Headset Headphones, Full RGB Light Design Gaming Headset Headphones with Mic, Gamer, Gaming, Gaming Headset Headphones, gaming headset wireless, Headphone, Headphones, Headset, Light, Mic, Package, RGB$24.99$14.99 -
Sale!
Wireless BlueTooth Multi-Device Keyboard Mouse Combo
$39.99$19.99 Add to cartWireless BlueTooth Multi-Device Keyboard Mouse Combo
Categories: Electronics, Gaming, Gaming Keyboards, Keyboard Mouse Combos Tags: Combo, Keyboard, keyboard mouse combos, Mouse, MultiDevice, Set, WireKeyboard Mouse Combo, Wireless, Wireless BlueTooth Keyboard Mouse Combo, Wireless BlueTooth Keyboard Mouse Combos, Wireless BlueTooth Multi-Device Keyboard Mouse Combo, Wireless BlueTooth Multi-Device Keyboard Mouse Combos$39.99$19.99 -
Sale!
High Back Leather Executive Adjustable Swivel Gaming Chair with Headrest and Lumbar
$199.99$139.99 Add to cartHigh Back Leather Executive Adjustable Swivel Gaming Chair with Headrest and Lumbar
Categories: Gaming, Gaming Chairs Tags: Adjustable, Chair, computer chairs, Desk, Executive, Gaming, Girl, Headrest, High, High Back Leather Executive Adjustable Swivel Gaming Chair, High Back Leather Executive Adjustable Swivel Gaming Chair with Headrest, High Back Leather Executive Adjustable Swivel Gaming Chair with Headrest and Lumbar, High Back Leather Executive Adjustable Swivel Gaming Chairs, Leather, Lumbar, Office, Racing, Swivel$199.99$139.99 -
Sale!
Professional LED Light Wired Gaming Headphones with Noise Cancelling Microphone
$29.99$19.99 Select optionsProfessional LED Light Wired Gaming Headphones with Noise Cancelling Microphone
SKU: N/A Categories: Electronics, Gaming, Gaming Headsets Tags: Cancelling, Gaming, Gaming Headphones with Noise Cancelling Microphone, gaming headset, Headphones, Headset, LED, Light, Mic, Microphone, Noise, Professional, Professional LED Light Wired Gaming Headphones, Professional LED Light Wired Gaming Headphones with Noise Cancelling Microphone, Wired, Wired Gaming Headphones, Wired Gaming Headphones with Noise Cancelling Microphone$29.99$19.99 -
Sale!
Gaming Desk with LED Lights USB Power Outlets and Charging Ports
$349.99$249.99 Select optionsGaming Desk with LED Lights USB Power Outlets and Charging Ports
SKU: N/A Categories: Computer Desk, Gaming, Gaming Desk Tags: and Charging Ports, Charging, Desk, Desks, Gaming, gaming desk with led lights, Gaming Desks with LED Lights, Home, LED, Lights, Monitor, Office, Outlets, Port, Power, Room, Stand, USB, USB Power Outlets, White, Workstation$349.99$249.99 -
Sale!
Wired Mixed Backlit Anti-Ghosting Gaming Keyboard
$99.99$79.99 Add to cartWired Mixed Backlit Anti-Ghosting Gaming Keyboard
Categories: Electronics, Gaming, Gaming Keyboards Tags: Antighosting, Backlit, Blue, brown, Gaming, Gaming Keyboard, gaming keyboards, gaming keyboards and mouse, Keyboard, Laptop, Switch, Wired, Wired Mixed Backlit Anti-Ghosting Gaming Keyboard, Wired Mixed Backlit Anti-Ghosting Gaming Keyboards, Wired Mixed Backlit Gaming Keyboard$99.99$79.99 -
Sale!
Wireless Bluetooth 5.3 ANC Noise Cancellation Hi-Res Over the Ear Headphones Headset
$119.99$59.99 Add to cartWireless Bluetooth 5.3 ANC Noise Cancellation Hi-Res Over the Ear Headphones Headset
Categories: Electronics, Gaming, Gaming Headsets Tags: 5.3 ANC Noise Cancellation Hi-Res Over the Ear Headphones Headset, ANC, Audio, Bluetooth, Cancellation, Ear, Earphone, gaming headset, Headphones, Headset, Hi-Res Over the Ear Headphones Headset, HiRes, Noise, Wireless, Wireless Bluetooth 5.3 ANC Noise Cancellation Hi-Res Headphones, Wireless Bluetooth 5.3 ANC Noise Cancellation Hi-Res Over the Ear Headphones Headset, Wireless Bluetooth 5.3 ANC Noise Cancellation Hi-Res Over the Ear Headphones Headsets$119.99$59.99 -
Sale!
Wired Sports Gaming Headset Earbuds with Microphone
$19.99$9.99 Select optionsWired Sports Gaming Headset Earbuds with Microphone
SKU: N/A Categories: Gaming, Gaming Headsets Tags: Accessories, Earbud, Earphone, Earphones, Gaming, gaming headset with microphone, Headphones, Headset, IOS, Microphone, Sports, Wired, Wired Sports Gaming Headset Earbuds, Wired Sports Gaming Headset Earbuds with Microphone, Wired Sports Headset Earbuds$19.99$9.99 -
Sale!
150W Universal Multi USB Fast Charger 16 Port MAX Charging Station
$49.99$29.99 Add to cart150W Universal Multi USB Fast Charger 16 Port MAX Charging Station
Categories: Charging Stations, Electronics Tags: 150W, 150W Charging Station, 150W Universal Multi USB Charging Station, 150W Universal Multi USB Fast Charger 16 Port MAX Charging Station, 150W Universal Multi USB Fast Charger 16 Port MAX Charging Stations, 150W Universal Multi USB MAX Charging Station, 16 Port MAX Charging Station, 3.5A, Charger, Charging, Fast, laptop charging stations, Max, Multi, Port, Stand, Station, Universal, USB$49.99$29.99
I am lost
Brilliant! Can I ask why you removed the "location" parameter, I thought that would have been needed?
I've seen this video and the next one,I wanna ask is there an actual advantage that each of them have over the other?
Previous video in playlist was not about how to create an Objekt and this video I cannot understand because it is abut another Thema 😅
Thank you for this video! If anyone could help me though; I understand the javascript code in this video and I understand JS is a scripting language as to why it's in the console. But what is this code drawing? Is it drawing a circle on the screen? Or I guess my question is… where is the circle going to be at?
Then classes came out in JS and built everything into one super method lmaoo
simple explanation, Great !
Very good explanation! As a guy who learned C++ now learning web development, I was trying to do OOP the way you would in C++ but I couldn't figure out how the JSON data I was working with have a variable named 'shoulder-length'. You can't use a hyphen in Classes… Found this video and immediately understood what was going on! I had never heard of factory functions.
Hi Mosh, great video!
Query: how do you add methods from OUDSIDE the function? Following your example in the video, I have used the below for both factory and constructor functions and it works, but I wonder if there is another way specifically for factory functions?
circle1.anotherDraw = function() {
console.log('Adding a new method');
}
circle1.anotherDraw();
Thanks, easy to understand even for non english speaking !
I thought the return would end the statement or function there at the top, isnt that how it was supposed to work ?
Hi! What would you recommend? Factory method or class factory???
Has anyone noticed one thing? When you call
createCircle(1).draw();
though, it prints the message 'draw', the type of the draw() function returned in the browser console is 'undefined'. Why is it? Should it not be 'function'? Please explain why.
Hi there, by declaring: function createCircle() { …. } aren't you attaching your function to the main Window and as I recall you said that is BAD PRACTICE? since this can create problems with your script if using libraries… or so…?
what do you use?
constructor or factory…
This is a good and clear lesson but I feel like you missed the encapsulation aspect of factory functions and how you can theoretically make methods and attributes private with it. That was the explanation I was looking for after reading some articles about it.
How do we call the location property? Within it, there are two sub properties (x, y) too.
Simple and intuitive. Great lesson!
isn't the draw function duplicated for each object this way?
great
it's so useful, thanks for great teaching!
Hey, factory function is similar to C programing ….. Ryt
Thanks I was a little stuck, this video helped it click. A few other videos I checked out weren't really explaining it simple enough. THANKS!
Explained very well!!