What returns this in Function.prototype.method?

I just started reading JavaScript: “Good Details”, and am I already confused by “returning this” to Function.prototype.method? I understand how 'this' and 'return' work. 'this' is essentially a pointer to the current object, and "return" just exits the function when the value is output, if you have described any; in our case, 'this'.

Here is the code I'm referring to.

Function.prototype.method = function(name, func) {
    this.prototype[name] = func;
    return this;
}

/* SIMPLE CONSTRUCTOR */
function Person(name, age) {
    this.name = name;
    this.age = age;
}

/* ADD METHODS */
Person.method('getName', function() { return this.name; });
Person.method('getAge', function() { return this.age; });

var rclark = new Person('Ryan Clark', 22);

console.log(rclark.getName()); // string(Ryan Clark)
console.log(rclark.getAge()); // number(22)

I tried to skip "return this" to see if the code breaks, but is it not? What exactly does “bring it back” do? I will continue this book, but I want to make sure that I understand everything. Any help would be appreciated.

+4
2

, - :

/* ADD METHODS */
Person.method('getName', function() { return this.name; })
      .method('getAge', function() { return this.age; });
+4

return this , method(), , , .

, , , , , :

Person.method('getName', function() { return this.name; }).method('getAge', function() { return this.age; });
+1

All Articles