Skip to content

Instantly share code, notes, and snippets.

@josher19
Created January 11, 2010 11:22
Show Gist options
  • Save josher19/274169 to your computer and use it in GitHub Desktop.
Save josher19/274169 to your computer and use it in GitHub Desktop.
//////// FIXED
// See BuggyClass.js for explanations of problems with doing
// default value assignment with || operator.
// gist: 274158 http://gist.github.com/gists/274158
/** A slightly better way to set default value, so `new FixedBuggyClass()` works as expected */
function FixedBuggyClass(param1, param2,param3) {
if (param2 == null) param1 = "defaultValue";
this.param1 = param1;
if (param2 == null) param2 = true;
this.param2 = param2;
if (param3 == null) param3 = -1;
this.param3 = param3;
this.input = param1 + ".." + param2; // lose original input, but matches this.derived now.
this.derived = this.param1 + ".." + this.param2;
this.same = this.derived == this.input; // should always be true, right?
}
FixedBuggyClass.prototype.unexpected = function() {
var res = [];
for (attr in this) {
var t = typeof(this[attr]);
if (t != "function" && t != "undefined") res.push(attr + "=" + this[attr]);
}
return res.join("\n")
}
FixedBuggyClass.prototype.toString = FixedBuggyClass.prototype.unexpected;
var show = self.consle ? console.log : function(msg) { alert(msg); }
var bugsMeDefault = new FixedBuggyClass(null, null)
// This is buggy:
var bugsMeNot = new FixedBuggyClass("ok", false,0);
///// May not get what you expect!!!
var results = [
bugsMeDefault,
bugsMeNot.unexpected(),
bugsMeNot.param1,
bugsMeNot.param2 == false,
bugsMeNot.param3 == 0,
].join("\n\n");
show(results);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment