Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Stephen Gehring
11,203 Pointsmerging constructor function json arguments into "this"?
Is there a way I can merging constructor function json arguments into "this"?
function Rule(type, options) {
// public properties
this.type = type;
this.space = 0;
this.count = 0;
this.length = 0;
this.increment = 1;
// private properties
var _ = {}
_.valid = false;
// want to merge "options {}" argument into "this" to for easier code clarity & and normalize of the options passed.
this.merge(options); // this is tossing a error
// a public method to get for private properties
this.get = function(name) {
return _[name];
}
// a public method to validate the tic tac toe rule
this.validate = function() {
//..
}
}
var myRule = new Rule('row', {space : 1, count: 0, length : 6, increment: 3});
2 Answers
Stephen Gehring
11,203 PointsHere is how I am solving this in the short term.
_.merge = function (options) {
for (var attrname in options) {
this[attrname] = options[attrname];
}
}
Steven Parker
243,656 PointsWhy not define merge as an instance method?
Instead of "_.merge = ...", just define it as "this.merge = ...".