Constructors and method chaining in JavaScript
Posted
by
Sethen Maleno
on Stack Overflow
See other posts from Stack Overflow
or by Sethen Maleno
Published on 2012-11-18T04:50:21Z
Indexed on
2012/11/18
5:00 UTC
Read the original article
Hit count: 115
JavaScript
I am trying to make method chaining work in conjunction with my constructors, but I am not exactly sure how to go about it. Here is my code thus far:
function Points(one, two, three) {
this.one = one;
this.two = two;
this.three = three;
}
Points.prototype = {
add: function() {
return this.result = this.one + this.two + this.three;
},
multiply: function() {
return this.result * 30;
}
}
var some = new Points(1, 1, 1);
console.log(some.add().multiply());
I am trying to call the multiply method on the return value of the add method. I know there is something obvious that I am not doing, but I am just not sure what it is.
Any thoughts?
© Stack Overflow or respective owner