I'm new to CoffeeScript, so I'm just using this to document idioms that I am using. Suggestions / improvements are very welcome.
increment = do ->
x = 0
$ node | |
> var _ = require('underscore-node'); | |
undefined | |
> _ | |
undefined | |
> var stampit = require('stampit'); | |
undefined | |
> stampit | |
{ [Function: stampit] | |
compose: [Function: compose], |
Nginx: | |
``` | |
173.245.55.164 - - [05/Nov/2014:18:46:21 -0500] "GET /js/index.js HTTP/1.1" 200 23893 "http://moonshotcon.com/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.111 Safari/537.36" | |
``` | |
Apache2 |
In JS FP do u ever extend objs with other objs? tweet
Yes, frequently to copy data from one collection to another during object creation, but once created and returned, objs are treated as immutable collections. Contrast against shared state with methods that mutate properties as in OOP. As a consequence of the immutable nature of collections in FP, pure functions boast stronger encapsulation features than private methods in OOP, because they guarantee against side-effects.
Unlike in OOP, most functions in FP are generic and independent of object properties and collection element contents, so there is typically no need to copy methods from object to object.
One major exception is that collections (or observable streams in FRP) may be enhanced with a number of functional utility methods which all return new values rather than mutating the properties of the parent object.
Do u have an example of FP vs OOP? tweet
Any example would be contrived, and will not adequately illustrate the fundamental philosophical differences between OOP and functional programming. For much more on pros and cons of OOP vs Functional programming, see "The Two Pillars of JavaScript" Part 1 (OOP) and Part 2 (Functional Programming).
OOP collection.append()
:
var collection = function collection(arr) {
return {
elements: [].slice.call(arr || []),
{ | |
"maxerr": 10, | |
"node": true, | |
"browser": true, | |
"typed": true, | |
"worker": true, | |
"browserify": true, | |
"predef": ["define", "require"], | |
"camelcase": false, | |
"curly": true, |
// A ray of sunshine in the belly of | |
// the beast... | |
var object = {}; | |
_.extend(object, Backbone.Events); | |
object.on("alert", function(msg) { | |
alert("Triggered " + msg); | |
}); |
var a = []; | |
var b = Object.create(a); | |
b.push('foo'); | |
b[0]; //=> 'foo' | |
b instanceof Array // => true | |
b.length // => 1 |
var myModule = (function () { | |
return { | |
hello: function hello() { | |
return 'Hello, world!'; | |
} | |
}; | |
}()); |