Skip to content

Instantly share code, notes, and snippets.

View CrabDude's full-sized avatar

Adam Crabtree CrabDude

View GitHub Profile
// NOT AN APPLICATION, I JUST WANTED TO TAKE YOUR TEST. =)
// 1: how could you rewrite the following to make it shorter?
if (foo) {
bar.doSomething(el);
} else {
bar.doSomethingElse(el);
}
// ANSWER
@CrabDude
CrabDude / inheritance.js
Created February 8, 2011 21:32
Private.js Example: Inheritable Private Instance Members
// Class.extend takes a function that returns the options object
// it is passed the accessor function generated in the Pvt.js example above
var Person = Class.extend(function(pvt) {
// private static member
var species = "Homo sapien";
return {
init: function(isDancing) {
// pvt(this).invisible === undefined
@CrabDude
CrabDude / basic.js
Created February 10, 2011 20:32
Pvt.js Example: Simple (Non-Inheritable) Private Instance Members
// Utilize a closure to keep the private members private
var Person = (function() {
// Generate a private instance member accessor function "pvt"
var pvt = Pvt(),
// private static member
species = "Homo sapien";
var Self = function(isDancing) {
@CrabDude
CrabDude / privates.js
Created February 10, 2011 20:34
Privates.js - JavaScript Inheritance With Inheritable Private Instance Members
/* Privates.js - Simple JavaScript Inheritance With Inheritable Private Variables
* By Adam Crabtree http://noderiety.com/
* MIT Licensed.
*
* Forked from:
* Simple JavaScript Inheritance
* By John Resig http://ejohn.org/
* http://ejohn.org/blog/simple-javascript-inheritance/
* Inspired by base2 and Prototype
*/
@CrabDude
CrabDude / pvt.js
Created February 10, 2011 20:34
Pvt.js - JavaScript Private Instance Members
/* Pvt.js - JavaScript Private Instance Members
* By Adam Crabtree http://noderiety.com/
* MIT Licensed.
*/
/**
* Generate a private member store
* @returns {function} The private member accessor function
*/
function Pvt() {
@CrabDude
CrabDude / crockford.js
Created February 10, 2011 21:40
Crockford's "Private Members in JavaScript"
// Public
function Constructor(...) {
this.membername = value;
}
Constructor.prototype.membername = value;
// Private
function Constructor(...) {
var that = this;
var membername = value;
@CrabDude
CrabDude / LICENSE.txt
Created May 27, 2011 16:46 — forked from 140bytes/LICENSE.txt
Force function as constructor
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Version 2, December 2004
Copyright (C) 2011 YOUR_NAME_HERE <YOUR_URL_HERE>
Everyone is permitted to copy and distribute verbatim or modified
copies of this license document, and changing it is allowed as long
as the name is changed.
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
@CrabDude
CrabDude / Function.prototype.makeCtor
Created July 7, 2011 23:47
Function.prototype.makeCtor: Guarantee Javascript function executes as a constructor.
Function.prototype.makeCtor = function() {
function ctor() {
if (!(this instanceof arguments.callee)) {
return new (arguments.callee.bind.apply(arguments.callee,Array.prototype.concat.apply([null],arguments)));
} else {
that.apply(this,arguments);
}
};
return (Function('var that = this; return '+ctor.toString().replace(ctor.name, this.name))).call(this);
}
@CrabDude
CrabDude / SubArray.js
Created July 23, 2011 01:49
Node.js Array subclass
// For context on difficulties of subclassing Array:
// See, http://perfectionkills.com/how-ecmascript-5-still-does-not-allow-to-subclass-an-array/
var sandbox = {};
require('vm').createScript('SubArray = Array').runInNewContext(sandbox);
var SubArray = sandbox.SubArray;
console.log(SubArray);
SubArray.prototype.__proto__ = {
__proto__: Array.prototype,
@CrabDude
CrabDude / formatStackTrace.js
Created September 22, 2011 19:07 — forked from creationix/formatStackTrace.js
Beginning of an event-source hook for system-wide stack handling hooks.
// Copyright 2006-2008 the V8 project authors. All rights reserved.
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following
// disclaimer in the documentation and/or other materials provided