Skip to content

Instantly share code, notes, and snippets.

@adamchester
Created July 25, 2012 03:27
Show Gist options
  • Save adamchester/3174193 to your computer and use it in GitHub Desktop.
Save adamchester/3174193 to your computer and use it in GitHub Desktop.
How do you create a CoffeeScript class instance using 'apply'?

How do you create a CoffeeScript class instance using 'apply'?

The answer is, like this:

util = require('util')
assert = require('assert')

class MyClass
	constructor: (@splatArgs...) -> @first = splatArgs[0]; @second = splatArgs[1]

describe 'apply on a CoffeeScript class', ->
	describe 'with splatArgs[1,2]', ->
		theInstance = {}
		MyClass.apply(theInstance, [1, 2])
		it 'should have a splatArgs.length of 2', -> assert.equal theInstance.splatArgs.length, 2
		it 'should have a first of value 1', -> assert.equal theInstance.first, 1
		it 'should have a second of value 2', -> assert.equal theInstance.second, 2
		it 'should have splatArgs of [1,2]', -> assert.deepEqual theInstance.splatArgs, [1,2]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment