15. Why are methods important? Do Objects have global scopes? Idk if that questionn makes sense


#1


https://www.codecademy.com/en/courses/spencer-sandbox/2/3?curriculum_id=506324b3a7dffd00020bf661


var bob = new Object();
bob.age = 17;
// this time we have added a method, setAge
bob.setAge = function (newAge){
  bob.age = newAge;
};

bob.getYearOfBirth = function () {
  return 2016 - bob.age;
};
console.log(bob.getYearOfBirth());

var getYearBorn = function() {
    return 2016 - bob.age;
    };
console.log(getYearBorn());
console.log(bob.age);

/*I don't know why a function I created called getYearBorn can do a methods job, even though it says that functions can only take parameters. getYearBorn did the same thing as getYearofBirth, even though it seems like lesson 13 is stating that functions can't do a methods job. I'm new to JS, so please forgive me if I have a huge gap in my logic. I'm just lost on this topic.*/


#2

It looks like the function you created is also a method?
This post has a better explanation than I can give!


#3

That's what I was thinking. Thanks for the confirmation.


#4

Short answer, methods (relative to their objects) are important because properties are static, where a property cannot get information dynamically, such as a property referencing another property of the same object, a method can, and return a dynamic & live value. If you update the property a method is pointing to, it'll reflect that new value each time, because a method is a function that runs when called. A property is like a variable, after it's been declared, it stores that value. Aside from that it just won't self reference the object.

There are two kinds of methods that do this for our properties, getters & setters. The syntax used to be a little unique, but now you can make getters & setters just like a regular method, don't worry about this unless you already know about them. All you need to know is that with a getter method, you can fetch a value from another property live just as I was describing, and with a setter you can update a value.

// Property failing to self-reference another property value
var object = {
  name: "michael",
  name2: this.name
}

object.name2; // Returns an empty string. FAIL! XD

// Method succeeding to self-reference another property value
var object = {
  name: "michael",
  name2() {
    return this.name
  }
}
object.name2(); // Returns "michael". Yeah!

// A classic getter method
var object = {
  name: "michael",
  get name2: function() {
    return this.name
  }
}
object.name2; // see how we don't need to include parenthesis?

It's also worth considering how useful it is to organize all of the actions for an object under one name creating a chain of references.

function getYearOfBirth() {}  // Contains no context as a foreign function
Person.getYearOfBirth();      // Holds a lot more context

If you want to be technical a lot of the methods or functions we use already belong to objects. The DOM, which makes up all web pages, is the Document Object Model. Nearly everything you see & know is an object in some way. There's a string object, a math object, and an array object. Objects rule!


"this" keyword Doubt in JAVASCRIPT
#5

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.