Constructors ( Object Oriented Programming in JavaScript Series - Part 1)

Поділитися
Вставка
  • Опубліковано 2 жов 2024
  • Creating objects with constructor functions.
    Article: techsith.com/ja...
    Code Samples:
    Constructor example:
    jsfiddle.net/j...
    Setting up private properties:
    jsfiddle.net/j...
    *My Udemy Courses
    www.udemy.com/...
    www.udemy.com/...
    Follow me for technology updates
    * / techsith
    * / techsith
    * / techsith1
    * / 13677140
    * / patelhemil
    Help me translate this video.
    * www.youtube.co...
    Note: use translate.goog... to translate this video to your language. Let me know once you do that so i can give you credit. Thank you in advance.

КОМЕНТАРІ • 126

  • @RezaMohseni
    @RezaMohseni 5 років тому +7

    Best JS instructor out here!!!
    Your examples are simple, and easy to grasp.
    Thank you!

  • @xuelianwang5300
    @xuelianwang5300 5 років тому +6

    Thanks Techsith! It is awesome that closure can also work on the parameters of the function. Did not notice that before. Learned a lot from you! Thank you!

  • @dhananjaymenon3601
    @dhananjaymenon3601 5 років тому +2

    I think you missed an important point with using the new operator, the new operator creates an empty object sets the context to 'this' and also implicitly returns the object.

  • @vedravula196
    @vedravula196 5 років тому +2

    new.target property is the latest thing i came across. In-depth explanation and your patience is mind-blowing

    • @Techsithtube
      @Techsithtube  5 років тому

      Yes there are lots of new features added to JS that are pretty cool and useful. Thanks for watching.

    • @ironhide9955
      @ironhide9955 5 років тому

      @@Techsithtube thank you bro

  • @fred1153
    @fred1153 4 роки тому +2

    Good video Techsith. Thank you. The setColor and getColor methods make sense. But it seems odd to be passing in _color to the constructor if _color is a private property. Wouldn't you want to pass in color to the constructor and then do 'this.setColor(color);' after the setColor declaration to initialize _color = color?

    • @Techsithtube
      @Techsithtube  4 роки тому

      Fred , what you said completely makes sense. _color should always be a local property that can not be accessed directly and the property that you pass should be named color. It's a bit old video and I just realized after re-watching it. Thanks for pointing it out.

  • @IndianinAmsterdam
    @IndianinAmsterdam 6 років тому +1

    Grest explaination!!!
    Keep doing....
    Please add tutorials for webpack and angular Inerview Q&A.

  • @PrudhviRajRaavi
    @PrudhviRajRaavi 4 роки тому +1

    You are very articulated about the concepts you're explaining thanks a lot man

  • @wombat7961
    @wombat7961 5 років тому +1

    Good stuff here TechSith, dont forget to add a link to Closures beneath the video ;)

  • @derrick7968
    @derrick7968 6 років тому +5

    so awsome..was almost quiting on javascript till i met your videos..thanks alot

    • @Techsithtube
      @Techsithtube  6 років тому +2

      I am glad that you decided to not quit Murithi! Keep learning !

    • @derrick7968
      @derrick7968 6 років тому

      ooh sure, from kenya by the way. i like your tutorials.

    • @derrick7968
      @derrick7968 6 років тому

      how do i destroy an object after instantiating from a class or a base constructor,?
      for example am building a media player using jquery and javascript.
      eg var audio = new Audio();
      at a certain stage i want to delete this audio object, how do i do it?

    • @Techsithtube
      @Techsithtube  6 років тому +1

      You can set it to null.

    • @derrick7968
      @derrick7968 6 років тому

      ooh i see..thanks

  • @sivacharan3987
    @sivacharan3987 2 роки тому

    Sir,I have a doubt in oop in JavaScript.when can implement anything in JavaScript without oop also.Then what is the use of oop in JavaScript?When do We use oop in JavaScript?

  • @sivacharan3987
    @sivacharan3987 2 роки тому

    Sir,I have a doubt in oop in JavaScript.when can implement anything in JavaScript without oop also.Then what is the use of oop in JavaScript?When do We use oop in JavaScript?

  • @sindhu1345
    @sindhu1345 5 років тому +2

    This is one of the most useful tutorials ever!! Thank you so much for this techsith tutorials 🙌

  • @fabriziogallici7118
    @fabriziogallici7118 4 роки тому

    Hi, why i saw on other tutorials getters and setters used like: get color() {return this._color} , or set color(color) {this._color = color} , i find your usage a bit confusing: is there any difference? thanks.

  • @ralpharnold9186
    @ralpharnold9186 Рік тому

    I would like it if you tough the virtual dom, shadow dom and vanilla javascript.

  • @HakanGalip
    @HakanGalip Рік тому

    Underscore generates not a private property it shows only a private characteristic... # at the beginning of a property is a private property. This is the case in newer versions of JS

  • @honghulin8810
    @honghulin8810 4 роки тому

    Hi sir, what will happen if I call method of a class inside of a constructor? Thank you !

  • @rohitmadhavi5833
    @rohitmadhavi5833 3 роки тому

    Tried to watch few of your videos. You seem to sway away from the actual topic. For example if a person is coming to this video to learn about constructor then to understand your explanation he or she has to also look out for new.target or closures.

  • @DaveHoran
    @DaveHoran 7 років тому +4

    Great start to the series. Looking forward to the rest.

  • @AE-yr6mo
    @AE-yr6mo 3 роки тому

    Do you know if it's possible to create a constructor function that takes an optional / unlimited set of parameters?

  • @reynandelizo1480
    @reynandelizo1480 4 роки тому

    I want to enroll in you javaScript Tutorial you are great teacher..

  • @Atif1702
    @Atif1702 5 років тому

    Can anyone please explain this? This code also works perfectly fine without setColor(). *Where is actually the color being set?*
    let Car = function(_color) {
    this.getColor = function() {
    return _color;
    }
    }
    var redCar = new Car('Blue');
    console.log(redCar.getColor()); //Blue

    • @Techsithtube
      @Techsithtube  5 років тому +1

      its using closure to hold _color. now, this works because the method is inside the function. if that was a prototype method it would not work.

  • @pravesh1903
    @pravesh1903 5 років тому +1

    Thank you so much. Very helpful !

  • @nafeesahmed4942
    @nafeesahmed4942 7 років тому +2

    You explained very well and i followed your all videos, what about if you creat a small project/ application which includes everything . It actually help to build a project/ architecture concepts from scratch

    • @Techsithtube
      @Techsithtube  7 років тому +2

      Towards the end of the series I will build a small practical project to explain it all. . Thanks for watching!

    • @nafeesahmed4942
      @nafeesahmed4942 7 років тому

      also one topic about Dependency Injection if it's possible for you to explain.

    • @Techsithtube
      @Techsithtube  7 років тому

      sure I will try to make one

  • @sevdamurat4455
    @sevdamurat4455 5 років тому

    Hey @techsit, why didnt you defined _color as a function variable, instead used it without defining as a function argument? (while teaching private variables...10:00)

    • @Techsithtube
      @Techsithtube  5 років тому

      _color That is used as a closure.

  • @blossom0417
    @blossom0417 6 років тому

    you are really A great javascript teacher! :)
    just a quick question.
    what is the difference return value and assign the value ?
    //return the value
    this.getColor = function(){
    return _color ; // return the value
    }
    //assign the value
    this.color = _color; // assign the value to property
    --- i've learned this code to assign the value
    I don't know any difference.
    please help me in understanding.
    thanks in advance.

  • @user-fg8nl3zg2h
    @user-fg8nl3zg2h 6 років тому

    Great Tutorial. i understand constructor but angular 2 use constructor this type " constructor(public fb:firebase){
    but not use here this.fb = firebase
    }"
    why i don't understand this. pls help me

  • @balledachandrahas8326
    @balledachandrahas8326 5 років тому

    Ur name shouldn't be techSith. It should've been techYoda..

  • @jtons7201
    @jtons7201 6 років тому

    is the 'global object: window' different from the 'master Object' ?

  • @lanc3068
    @lanc3068 5 років тому

    Tech Smith please make a video on cookies of programming😂😎

  • @steadyannie3467
    @steadyannie3467 7 років тому +2

    thanks, very good tutorial.

  • @ragav67
    @ragav67 7 років тому +2

    Easy to understand. Thank you :)

  • @sonaighosh
    @sonaighosh 5 років тому +1

    Very helpful tutorial..

  • @TheSimeon1111
    @TheSimeon1111 6 років тому

    Please be decent and stop promoting the old JS syntax. Please use a decent syntax version according to our year. DO NOT MISLEAD NEWBIES!

    • @Techsithtube
      @Techsithtube  6 років тому

      First of all I am decent , second of all I am not promoting any thing, I simply teach . And I still don't understand what old JS syntax I am referring to? I use 'let' not 'var' also every function is a constructor. if you learn react you will know the binding issue with 'this' that is because most of the people dont know about function as a constructor.

  • @praneetrane2993
    @praneetrane2993 6 років тому

    Couple of days back my friend recommended me to watch JavaScript videos on this channel.Thanks for the tutorials with details explanation.I am already fan of these tutorial.Very eager to watch complete JavaScript series today itself.

  • @vaclavvlcek4527
    @vaclavvlcek4527 3 роки тому

    Thank you very much for your videos. Your really start from the very basics so it is comprehensive also for the complete newbies. Just one tiny advice. The newbies won't probably know how to get the same result as you have in the console :) That jsfiddle.net what you recomended once is a great shortcut.

    • @Techsithtube
      @Techsithtube  3 роки тому

      Vaclav, Thanks for the advice, I will keep it mind for the next video . Thank you for watching!

  • @threeone6012
    @threeone6012 7 років тому

    Excellent video I'm looking forward to video #2. By the way I don't know if you have lived in the USA for a long time but you speak excellent English. I understood everything you said perfectly. Thanks for the upload!

  • @trappedcat3615
    @trappedcat3615 7 років тому +2

    cant stand this unless its const that = this

    • @trappedcat3615
      @trappedcat3615 7 років тому

      no...just storing this to a constant named that. It can be named anything for clarity, but it should not be reassigned a value.

    • @Techsithtube
      @Techsithtube  7 років тому

      Its a good idea to use const instead of let usually. So you don't accidentally overwrite.

  • @davitjanashia9344
    @davitjanashia9344 4 роки тому

    Thank you very much for these wonderful videos! They are very clear and helpful! Good luck

  • @prasadbagade5541
    @prasadbagade5541 5 років тому

    Your explanation is just awesome and simple. Thanks a lot !!!

  • @anonymous.youtuber
    @anonymous.youtuber 4 роки тому

    Awesome ! 👍🙏🏻

  • @shaikusman536
    @shaikusman536 6 років тому

    Very well explained and Great Tutorials bro...

  • @yaswanthkrishna2154
    @yaswanthkrishna2154 7 років тому

    thank you for sharing this tutorial actually I am having one exam on object oriented JavaScript this Saturday could you please tell me some websites.. to learn and write the exam very effectively

    • @yaswanthkrishna2154
      @yaswanthkrishna2154 7 років тому

      thank you so much for providing these details but the way it was explained in Mozilla , exploring js is very difficult to understand . your videos are very simple enough to understand the same

  • @ReneBroekhoven
    @ReneBroekhoven 7 років тому +2

    Very good, thanks. One question: Am I right is supposing that the new keyword "automatically" calls the this.setColor function (or really any function after the this keyword) ? Otherwise, it is very strange that you can console.log the getColor without having explicitly called first the setColor.

    • @Techsithtube
      @Techsithtube  7 років тому

      I am assuming that you are referring to the setting private property example. In that when you call the constructor if you notice the passed parameter is '_color' which is set the variable we are setting as a closure. So by simply passing it we are setting it. It doesn't really invoke the setColor function . Did i answer your question?

    • @ReneBroekhoven
      @ReneBroekhoven 7 років тому +1

      I am sorry : "this.setColor = function(color){_color = color}" is a function expression. But it is not invoked or called. How then can _color be set ? ( For which I am assuming the new keyword does the job) Thank you so much if you could enlighten me on this one (all your other great videos are always compeletly clear to me)

    • @Techsithtube
      @Techsithtube  7 років тому +2

      Yes this.setColor is not invoked automatically when the constructor is called. If you look at the example i don't have a local variable. I am passing _color to constructor and that automatically becomes a closure so you dont really have to set _color using setColor. However you can call setColor if you want to change the color at some point.

    • @ReneBroekhoven
      @ReneBroekhoven 7 років тому

      Thanks a lot ! Great work you are doing.

    • @syeddakar3560
      @syeddakar3560 7 років тому

      I had this same question as well. I couldn't figure out how "_color" was being set if setColor was never called.

  • @chandrakantht8710
    @chandrakantht8710 7 років тому

    Your Tutorials are really great with well explanation. Please provide the advanced Java Script Tutorials. That will help us most.
    Please make tutorials on JavaScript Oops concepts..

    • @Techsithtube
      @Techsithtube  7 років тому

      This series will go into JavaScript Oops concepts. Which other advanced tutorial you would like to see?

  • @WidlyScuds
    @WidlyScuds 5 років тому

    The force is strong in you techSith.

    • @Techsithtube
      @Techsithtube  5 років тому

      May the force be with you Aaron. :)

  • @shsoh6726
    @shsoh6726 7 років тому

    Come-on, Tree House F.u.C.K.e.D me up, I hate advertisements

    • @Techsithtube
      @Techsithtube  7 років тому

      I know somehow treehouse is throwing money like there is no tomorrow. :)

  • @kamaboko1
    @kamaboko1 7 років тому

    Hi,
    A question for you. All the documentation I've been able to find for creating constructors shows something like the following (using your Car example):
    function Car (color) {
    this.color = color;
    };
    I'm just sort of confused why you use let, function and = to start it out. Is this more of a style thing?
    let Car = function(color){
    this.color = color;
    };
    Thanks for your input.

    • @Techsithtube
      @Techsithtube  7 років тому +2

      when you say function Car (color) {
      this.color = color;
      };
      its called function declaration
      let Car = function(color){
      this.color = color;
      }; is called function expression . where function is stored into a variable.
      In JavaScript, function expression is preferred over function declaration. because function expressions are not hoisted. I have a tutorial on it where I explain the difference in detail.

    • @kamaboko1
      @kamaboko1 7 років тому

      OK, thanks for your reply.

  • @shaikusman536
    @shaikusman536 6 років тому

    Pls Suggest...What programming languages one needs to know to understand React Native.......

    • @Techsithtube
      @Techsithtube  6 років тому +1

      You need to have strong JavaScript base and you can learn React Native.

    • @shaikusman536
      @shaikusman536 6 років тому

      Thanks bro... i am learning from your Java script tutorials, Js Basics,Advance,OOps i mean your every tutorial on JS....Hope that will be enough to build apps on React Native...

  • @returnZeroo
    @returnZeroo 5 років тому

    Best JS OOP series I have seen so far

  • @ramanr9935
    @ramanr9935 6 років тому

    Hi bro i like your videos i have doubt in return function can u make separate vds .

  • @anthonyrodriguez3645
    @anthonyrodriguez3645 6 років тому

    Great Tutorials! I'm doing interview questions and each question that comes up you have the answers for. What ide are you using that completes your div tags ?

    • @Techsithtube
      @Techsithtube  6 років тому

      I am using jsfiddle . But with any ide u can type div and hit tab to complete the tag

  • @coolito123
    @coolito123 6 років тому

    Thanks a lot for this series!! You're the man.

  • @ashwini4444
    @ashwini4444 5 років тому

    well explained thank you sir

  • @eltacobanido6307
    @eltacobanido6307 5 років тому

    Your webcam bitrate is really low

    • @Techsithtube
      @Techsithtube  5 років тому

      Yes it was supposed to be the best webcam. but turned out to be completely useless.

    • @eltacobanido6307
      @eltacobanido6307 5 років тому

      @@Techsithtube could be Just config

  • @RajeshWellington
    @RajeshWellington 7 років тому

    Can you create playlist for React + Redux + Webpack + Babel?

    • @Techsithtube
      @Techsithtube  7 років тому +2

      I am going to start React series after this series.

    • @RajeshWellington
      @RajeshWellington 7 років тому

      Thank you.. Awesome.. waiting for it

  • @bozodeniro
    @bozodeniro 6 років тому +2

    Not loud enough, needs more volume, and a volume control, and UA-cam widgets where they belong.

  • @maskman4821
    @maskman4821 7 років тому +1

    very well explained!!!

  • @buffalostreet
    @buffalostreet 5 років тому

    Thank you

  • @mahesh82398
    @mahesh82398 7 років тому +1

    Nice tutorial :)

  • @prathisahrudh
    @prathisahrudh 4 роки тому

    part 2 ?

    • @Techsithtube
      @Techsithtube  4 роки тому

      Parthi, here is the full playlist ua-cam.com/play/PL7pEw9n3GkoW0ceMeoycg9D00YjPAbtvt.html

  • @DeepSingh-qd7ed
    @DeepSingh-qd7ed 7 років тому

    very well explained. thanks

  • @djpacu
    @djpacu 6 років тому

    thanks

  • @seenuvasanv
    @seenuvasanv 6 років тому

    Thanks.

  • @dmytrokulikov2489
    @dmytrokulikov2489 7 років тому

    Good job, thanks for Your work!

  • @sabareeshk976
    @sabareeshk976 7 років тому

    keep inspiring

  • @shahrukh631
    @shahrukh631 7 років тому

    I'm excited

  • @SivaKumar-zu1hc
    @SivaKumar-zu1hc 6 років тому

    Hi Techsith. Can u suggest me best way to practice while watching your videos like you are typing sample codes and showing output. I want the same kind.

  • @betteratcoding
    @betteratcoding 7 років тому

    This is awesome. I have been working as a web developer for several years now and I can say from experience that this is extremely clear and to the point.