• Tuesday , 21 November 2017

Beginner JavaScript Tutorial – 27 – Adding Methods to Our Objects

Code Canyon



Facebook – https://www.facebook.com/TheNewBoston-464114846956315/
GitHub – https://github.com/buckyroberts
Google+ – https://plus.google.com/+BuckyRoberts
LinkedIn – https://www.linkedin.com/in/buckyroberts
reddit – https://www.reddit.com/r/thenewboston/
Support – https://www.patreon.com/thenewboston
thenewboston – https://thenewboston.com/
Twitter – https://twitter.com/bucky_roberts

Original source

3d Ocean

Related Posts

33 Comments

  1. Bikash Majumdar
    September 5, 2017 at 08:02

    Why isn't my browser processing this ?
    ?

  2. Sheni Hamitaj
    September 5, 2017 at 08:02

    this syntax is … so alienish

  3. TheWeakHero
    September 5, 2017 at 08:02

    i did:
    function people(year) {
    this.year = year;
    this.yourage = getage;

    }

    function getage() {
    return 2017 – this.year;

    }

    var hamid = new people(2002);

    document.write(hamid.yourage);

    it gave me this error at the browser pls help:
    function getage() { return 2017; }

  4. Albert Chan
    September 5, 2017 at 08:02

    Ummm… Why is yearsLeft() not encapsulated (not even sure if it's the right term) inside people class ?
    from my experiment, not putting yearsLeft() inside people class function means that function yearsLeft() can be called from anywhere by anyone, and thus creating unexpected and undesirable result—especially when playing with "this" keyword.
    putting yearsLeft() inside people class straight up preventing other irrelevant things from accessing yearsLeft() function without instantiating people class first.

  5. Shashwat Dahal
    September 5, 2017 at 08:02

    I don't know whats going on the 100 is not subtracting this.agehere is what I did<html>
    <head>
    <script type="text/javascript">
    function people(name,age){
          this.name = name;
       this.age = age;
       this.YearsUntilDeath = yearsLeft;
    }
         function yearsLeft(){
      numYears = 100 – this.age;
      return = numYears;
      }
     
      var youtube  = new people("Youtube channel" , 70);
    </script>
    </head>
    <body>
    <script type="text/javascript">
    document.write(youtube.yearsUntilDeath());
    </script>
    </body>
    </html>

  6. lawrence oguchi
    September 5, 2017 at 08:02

    now am getting lost

  7. Nakul Singh
    September 5, 2017 at 08:02

    he is giving best tutorials but i'm bored about objects and all that stuff 😴

  8. dzamija922
    September 5, 2017 at 08:02

    This guy's really got some issues with phlegm.

  9. Bill Beds
    September 5, 2017 at 08:02

    your code is wrong.

  10. MaxE Production
    September 5, 2017 at 08:02

    Can you do this.yearsLeft = yearsLeft? Or do they have to be different in this case?

  11. E. F.
    September 5, 2017 at 08:02

    What's wrong with this code? I keep getting the lines of code from function UrAgeMinusFive?

    function person(name, age) {
    this.name = name;
    this.age = age
    this.age2 = UrAgeMinusFive;
    }

    var a = new person("Bill", 25);
    var b = new person("Mark", 28);

    function UrAgeMinusFive() {
    var MinusFive = this.age – 5;
    return MinusFive
    }
    alert(a.age2)

  12. Bishwanath Yengkhom
    September 5, 2017 at 08:02

    hey Bucky I have a question why you are yearuntilRetire () … paranthesis .. will u please elaborate

  13. De'von Mackey
    September 5, 2017 at 08:02

    Where the parameters added to the document.write statement because yearsUntilRetire is = to a function() ???

  14. The Shroom
    September 5, 2017 at 08:02

    Seems like a weird attempt at implementing classes lol

  15. Mike Putz
    September 5, 2017 at 08:02

    A suggestion to anyone who watched this once (or twice). Try explaining it while you type out the code. After one go through, I emailed my dad and explained what each line meant. Why you use 'this.property', why you don't use parenthesis for 'yearsUntil' in the constructor etc. I had to sneak a peak a few times. Then watch the video again, taking notes. Finally, try doing it yourself without cheating. After the first watch through I was a bit lost, but 20 minutes later, 2 times watching it, and 3 times writing the code I feel more confident. Good luck everyone!

  16. lax Kan
    September 5, 2017 at 08:02

    For

    this.yearUntillRetire = yearsLeft;

    you dont need a braces here because it is a DELEGATE. "yearsLeft" is called a delegate, you can think it as a Function Variable(variable that represents a function). It can also be passed around like variables and any delegate that it was assigned to can be used as a function and that could result in "yearsLeft"' function to be called

  17. lax Kan
    September 5, 2017 at 08:02

    Dude I liked your Tutorial.. easier than reading a book. Which these days are becoming fatter and fatter.. like people. I didn't want to discourage you but official defining an object is mentioned as something like this. firstName, lastName are properties and fullName is a method

    var person = {
    firstName: "John",
    lastName : "Doe",
    id : 5566,
    fullName : function() {
    return this.firstName + " " + this.lastName;
    }
    };

    of course there are many ways to define an object

  18. WolvesFanVideos
    September 5, 2017 at 08:02

    really could do with learning this shit but its so hard

  19. chiranjib mukherjee
    September 5, 2017 at 08:02

    I didn't understand why haven't you put parenthesis when you assign this.yeaUntilRetire= yearLeft and you put parenthesis when you call yearLeft in body. Can anyone plz help me .

  20. Erika Nygard
    September 5, 2017 at 08:02

    I think you are a good teacher. Thank you for helping me understand this all better.

  21. darkFo3
    September 5, 2017 at 08:02

    Fahrenheit, Kelvin… He forgot calculus XD

  22. Marija Jakovljevic
    September 5, 2017 at 08:02

    Thank you for not making this a monster. Simple well understandable language, I like it! Keep up 🙂

  23. kkevin369
    September 5, 2017 at 08:02

    i thought functions didn't have scope with one another

  24. Vincent Lim Heng Yip
    September 5, 2017 at 08:02

    Can i ask a question?
    this.yearsUntilRetire = yearsLeft; // why there is not a bracket yearsLeft() .Is it mean that if we call a function without input any parameter ,so no need to put () ?

  25. L M
    September 5, 2017 at 08:02

    Wow now it piece of cake to learn the complexity data types behind function & objects thank Bucky you save me from a brain seizure 🙂

  26. B SomeMan
    September 5, 2017 at 08:02

    nice video

  27. Vishuwat Rawat
    September 5, 2017 at 08:02

    if the object's property is taking a function as it's value then that object property will become a method.

  28. Sam Kand
    September 5, 2017 at 08:02

    If you wanna write same code with Object Initializer:

    <html>
    <head>
    <script type="text/javascript">

    bucky = {name:"Bucky Roberts", age:24};

    retireYear = {rYear:65};

    </script>
    </head>
    <body>
    <script type="text/javascript">

    document.write(retireYear.rYear – bucky.age);

    </script>
    </body>
    </html>

  29. yerrapotu kiran
    September 5, 2017 at 08:02

    Y is he using two script tags ,one in head and one in body ,sorry for my ignorance

  30. Tarık Nural
    September 5, 2017 at 08:02

    For the people who is suffering about passing parameter without instantiation into function(class looking methods lol), you have to initialize every passed parameter inside the function with a local variable, which is variable declared inside the function, otherwise Javascript does not recognize what you are trying to do with that parameter. The thing confuses me is that maybe I got that parameter to use in a function inside the class and I don't want to store it. Here is what I am trying to say :

    function circle(radius,pi){
    this.pi = pi; // if you don't declare "this.pi"(which clearly I don't want to
    //waste memory by storing it)
    this.radius = radius; // it does not compile.
    this.area = calculateArea;

    }

    function calculateArea(){
    return this.pi*this.radius*this.radius;
    }

    var object = new circle(5,3.14);
    document.write(object.area());

  31. Michele Turchiarelli
    September 5, 2017 at 08:02

    Javascript syntax is so dumb…

  32. Guilherme Vittorazzi
    September 5, 2017 at 08:02

    pretty cool han!? 😀

  33. Hezha Manmi
    September 5, 2017 at 08:02

    Amazingly helpful! Super clear and easy to understand! Thanks.

Leave A Comment

You must be logged in to post a comment.