Skip to content

Instantly share code, notes, and snippets.

unless Function::bind?
Function::bind = (scope, args...) ->
target = this
if typeof target isnt "function" then throw new TypeError
bound = ->
unless this intanceof bound
return target.apply scope, [args..., arguments...]
F = ->
F.prototype = target.prototype
self = new F
commit 30b81beab62b14fb9bc81240cb9e91a57d9baf5e
Author: Michael Ficarra <git@michael.ficarra.me>
Date: Mon Aug 9 17:12:28 2010 -0400
don't require the *protect* instance method when we can use
Function.protect instead
diff --git a/Source/Core/Core.js b/Source/Core/Core.js
index 9eb9f68..30e0b92 100644
--- a/Source/Core/Core.js
Function.TRACE_ALL = Function.TRACE_NONE = 0
constants = [
'TRACE_ARGUMENTS'
'TRACE_CONTEXT'
'TRACE_RETURN'
'TRACE_TIME'
'TRACE_STACK'
]
for constant, n in constants
# calculate powers of two to assign to the constants. this allows
@michaelficarra
michaelficarra / example.js
Created October 6, 2010 03:53
JS classical OOP
__extends = function(parent, child) {
var ctor = function(){};
ctor.prototype = parent.prototype;
child.prototype = new ctor;
child.prototype.constructor = child;
if(typeof parent.extended === 'function') parent.extended(child);
child.__super__ = parent.prototype;
return child;
};
@michaelficarra
michaelficarra / gist:742076
Created December 15, 2010 15:22
test_conditional_with_comment_bodies
if false
#comment
else if true
###
block comment
###
else
arrayEq = (a, b) ->
if a is b
# 0 isnt -0
a isnt 0 or 1/a is 1/b
else if a instanceof Array and b instanceof Array
return no unless a.length is b.length
return no for el, idx in a when not arrayEq el, b[idx]
yes
else
# NaN is NaN
@michaelficarra
michaelficarra / gist:746710
Created December 18, 2010 17:56
forkNode
forkNode = ->
args = process.argv[1..]
nodeArgs = []
while (idx = args.indexOf '--nodejs') > -1
nodeArgs = nodeArgs.concat args.splice(idx,2)[1].split(/\s+/)
spawn process.execPath, nodeArgs.concat(args),
cwd: process.cwd()
env: process.env
customFds: [0, 1, 2]
for x, index in [1, 2, 3]
func = ->
console.log index
index++
for x, index in [1, 2, 3]
console.log index
index++
@michaelficarra
michaelficarra / dynamic_instantiation.js
Created December 19, 2010 19:58
instantiating a member of a class with a variable number of constructor arguments
var F = function(){};
F.prototype = klass.prototype;
var o = new F();
klass.apply(o,arguments)
return o;