Beginner JavaScript Tutorial – 27 – Adding Methods to Our Objects




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


33 responses to “Beginner JavaScript Tutorial – 27 – Adding Methods to Our Objects”

  1. 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; }

  2. 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.

  3. 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>

  4. 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)

  5. 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!

  6. 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

  7. 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

  8. 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>

  9. 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());

Leave a Reply