Skip to content

Instantly share code, notes, and snippets.

//////// 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;
/** 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:
// 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;
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.
// Boolean Quiz
var bool_examples = [
true == new Boolean(true),
true === new Boolean(true),
false == new Boolean(false),
false === new Boolean(false),
! 0,
! new Boolean(false),
! "",
Discussion about the "truthfulness" of Objects of the Boolean class.
Given some Javascript code of:
var bool_examples = [
true == new Boolean(true),
true === new Boolean(true),
false == new Boolean(false),
false === new Boolean(false),
! 0,
/**
* isType return true if obj is of class klass or a superclass of klass (such as Object).
* If type is a Function, will match if obj instanceof type (includes subclasses).
* If type is a String, will match if typeof(obj) === type or obj.constructor.name === type,
* so if tom is a Horse, which is a subclass of Animal,
* then isClass(tom, "Animal") is false but isClass(tom, Animal) is true.
* @Param {obj} Object to test
* @Param {type} Function or String with name or constructor of object.
* @Return {boolean} true if obj is of given type, using typeof, instanceof, or obj.constructor.
*/
/** Adding underscores template to the String object */
// Source: http://github.com/documentcloud/underscore
var _ = self._ || {};
// JavaScript templating a-la ERB, pilfered from John Resig's
// "Secrets of the JavaScript Ninja", page 83.
// Single-quote fix from Rick Strahl's version.
_.template = function(str, data) {
@josher19
josher19 / gweather_xml.js
Created February 1, 2010 01:56
xml to javascript
// Get leaf data
// or could iterate over leaf.attributes
function show(leaf,ind) { return [leaf.tagName, leaf.getAttribute ? leaf.getAttribute("data") : leaf.textContent]; }
function grab(node,indented) {
var ind=indented||"";
return Array.prototype.slice.call(node).map( function(item) {
var others = "";
if (item.childNodes && item.childNodes.length > 0) others = grab(item.childNodes, ind + " ");
return ind + show(item).join(": ") +"\n"+ others