JavaScript Question: What is a Factory Function?

แชร์
ฝัง
  • เผยแพร่เมื่อ 23 ก.ค. 2024
  • What is a factory function? In this tutorial we are going to talk about what they are and when you might want to use them. We will also address the value of closure with factory functions.
    Would you like to help keep this channel going?
    allthingsjavascript.com/contri...
    Access to EVERY course (get 2 months free): www.skillshare.com/r/profile/...
    Courses offered on Udemy at a discount (access from my site): allthingsjavascript.com/course...
    Tutorials referred to in this video:
    Understanding Closure: • Demystifying JavaScrip...
    Constructor Playlist: • JavaScript Constructors
    Prototype Playlist: • JavaScript Prototypes
    Using querySelector: • Selecting DOM Elements...
    Callback Pattern: • JavaScript Patterns: T...
    Higher Order Functions: • Higher Order Functions...
    For more resources on JavaScript:
    www.allthingsjavascript.com
    #javascript #AllThingsJavaScriptLLC

ความคิดเห็น • 23

  • @russelldriver6394
    @russelldriver6394 3 ปีที่แล้ว +2

    Nothing like that first javascript lesson of the day with a cup of coffee :)

  • @MauriceLacerda0
    @MauriceLacerda0 4 ปีที่แล้ว +7

    The other day I was recommending your channel and courses on Twitter or Instagram (can't remember which), and it went something like this: "Do you know the renowned physicist Prof. Richard Feynman, A.K.A The Great Explainer? Well, Prof. Hancock is basically the Richard Feynman of JavaScript. Go watch his tutorials."

    • @AllThingsJavaScript
      @AllThingsJavaScript  4 ปีที่แล้ว +1

      Wow! Mauricio thanks for the compliment. It really means a lot!

  • @victorlazo3808
    @victorlazo3808 4 ปีที่แล้ว +1

    You are a great teacher, i do really enjoy every video. Thanks for every one of them.!!!

  • @shaneholmes6218
    @shaneholmes6218 4 ปีที่แล้ว

    Awesome! Thank you!

  • @arunkaiser
    @arunkaiser 4 ปีที่แล้ว +3

    Proud to give first like and comment for this video 🎥 thank u so much 🙏🏽

    • @raymondmichael4987
      @raymondmichael4987 4 ปีที่แล้ว +1

      Arun Raj Moorthy, 🤣🤣🤣
      Proud to be the first to like and reply a first comment to this wonderful channel.
      Greetings from Tanzania 🇹🇿

    • @arunkaiser
      @arunkaiser 4 ปีที่แล้ว

      @@raymondmichael4987 😂😅😆

  • @Amal-qo6kx
    @Amal-qo6kx 4 ปีที่แล้ว

    very helpful thanks a lot :)

  • @batchrocketproject4720
    @batchrocketproject4720 ปีที่แล้ว

    Really nice comparisons, thanks. Although I believe it to be deprecated and discouraged, a prototype can be easily assigned in a factory function by including a __proto__ property, set to the name of the prototype object, in the returned object. I've always found this to be convenient although wish there were a modern equivalent that could also be written directly into the return object (I haven't found a way to implement the setPrototypeOf object method from within the object sensibly and have to apply it to each instance of the object after it's been returned).

    • @AllThingsJavaScript
      @AllThingsJavaScript  ปีที่แล้ว

      Yes, you are correct that using that method is discouraged, but you are right that it would be nice.

  • @veronnahkur992
    @veronnahkur992 6 หลายเดือนก่อน

    In your example, you could also use a closure inside the Greeting2 by using the 'greet' parameter instead of 'this.greeting'. Otherwise great introduction, keep up the good work.

  • @maheshgoud6402
    @maheshgoud6402 4 ปีที่แล้ว

    You are the best teacher
    Please Make videos on 2d arrays problems and Jquery videos...

    • @AllThingsJavaScript
      @AllThingsJavaScript  4 ปีที่แล้ว

      Here is a playlist on arrays. It may contain some array problems you are looking for: th-cam.com/play/PLTo9PCskHpbH8qMjsHZZZwgooQg_l25XT.html

  • @Luxcium
    @Luxcium 4 ปีที่แล้ว

    Can I request a NodeJs video about building a custom REPL and the context objects please 😄 Thank for your amazing work...

    • @AllThingsJavaScript
      @AllThingsJavaScript  4 ปีที่แล้ว

      Would this be for testing code? Just looking for a little bit of context.

    • @Luxcium
      @Luxcium 4 ปีที่แล้ว

      All Things JavaScript, LLC a tool that could be used to either test code or to query (API/DB/Backstore) using JS

    • @AllThingsJavaScript
      @AllThingsJavaScript  4 ปีที่แล้ว

      I will need to look into that. Haven't built something like that, but it does sound intriguing.

  • @christiantraylor8705
    @christiantraylor8705 3 ปีที่แล้ว

    why does it not return automatically, like when you first initialize ffun = factoryFun(5)? my logic coming from other PLs is that right there after that line ffun would equal 500.

    • @AllThingsJavaScript
      @AllThingsJavaScript  3 ปีที่แล้ว

      The return keyword determines what is returned from the function. If you look at the return keyword, it is returning an object. That object has a method inside that when invoked will return the value.

    • @christiantraylor8705
      @christiantraylor8705 3 ปีที่แล้ว

      @@AllThingsJavaScript i see. also, is there are reason you're using var and not let when instantiating some of these objects?

    • @AllThingsJavaScript
      @AllThingsJavaScript  3 ปีที่แล้ว

      @@christiantraylor8705 I use let more frequently now, but on the global space I will generally use var. It is important to know the difference though.