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;
}
function Person(name, age) {
this.name = name;
this.age = age;
}
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());
console.log(rclark.getAge());
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.