This file contains 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: |
This file contains 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 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 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 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
// 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), | |
! "", |
This file contains 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
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, |
This file contains 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
/** | |
* 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. | |
*/ |
This file contains 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
/** 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) { |
This file contains 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
// 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 |
This file contains 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
var proplist = _.keys(_); | |
var undef | |
proplist.push(undef); | |
proplist.push(null); | |
proplist = proplist.concat(proplist); | |
// proplist.push(proplist); | |
_.isNewFunction = function (obj) { return typeof obj === 'function' } | |
_.isA = function isA(o,c) {return o==null ? o===c : (o.constructor===c || ( _.isFunction(c) && o instanceof c));} |
OlderNewer