Is set.age a standard JavaScript method?


#1

Just wondering if set.age is a standard method or did we create a method?
I tried searching for more information on set.age but no luck!


#2

Short answer, as you have written it, no. The new JS (ES6) does have a built-in Special Object, or class, Set. But this is not about setting things. Rather it's for working with sets. And it doesn't have a property or method, .age.

That's the general gist. If this is the lesson I'm thinking it is (a link in your post is immensely helpful, hint, hint) we start by creating a global function,

var setAge = function (newAge) {

};

and then we assign the function to an object property.

var person = {};
person.setAge = setAge;

Now to accommodate this, the original function exposes its this variable.

    this.age = newAge;

which gives object execution context to our person object when we invoke the method on the object. The age property of that object is what gets set.

person.setAge(25);
console.log(person.age);   // 25

#3

@mtf - thanks for your inputs. Although, I understand what you are conveying, I need further clarification specific to my issue.

var bob = {
    age: 40
    };
bob.setAge = function (newAge) {
    return bob.age = newAge;
    };
console.log (bob.setAge(20));

Here, if I replace the method setAge with myAge, bobsAge, or something like that, I am getting error msg. So wondering if setAge is a standard method in JS


#4

No, it is not part of or built into JS. We have to write it ourself.