This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
I have some suggestions for making CoffeeScript Classes even easier to use | |
in a friendly, "Unfancy" way. | |
First, is to have an easy way to give the default value. | |
Second, is a quicker way set my class variables: | |
this.var_name = var_name; | |
Third, is a "mustbe" clause which ideally would be checked at compile time instead of while running. | |
Fourth, some examples of how these changes will make the code more "Unfancy", readable, and less buggy. | |
1.) Allow a "default" in function args or ?? in expressions as a shortcut. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//////// 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 file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Common JavaScript error for parameter "shortcuts". | |
// Be careful when you do this shortcut to assign default values! | |
function BuggyClass(param1, param2,param3) { | |
this.param1 = param1 || "defaultValue" | |
this.param2 = param2 || true; | |
this.param3 = param3 || -1; | |
this.input = param1 + ".." + param2; | |
this.derived = this.param1 + ".." + this.param2; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** testClass return true if obj is of class klass or a superclass of klass (such as Object). */ | |
function testClass(obj, klass) { | |
// try { | |
if (obj === null) return "null" === klass || null === klass; | |
if (typeof(obj) === "undefined") return "undefined" === klass || obj === klass; | |
if (typeof(obj) === klass) return true; // if klass is a String | |
//// uncomment next line to make testClass(5, "NUMBER") return true | |
if (typeof(obj) === klass.toString().toLowerCase()) return true; | |
if (obj.constructor === klass) return true; | |
//// uncomment next line to make testClass([], Object) return false: |
NewerOlder