Skip to content

Instantly share code, notes, and snippets.

@h2ospace
h2ospace / find.html
Created March 14, 2013 19:28
underscore: find, filter
(function() {
var a = [2, 5, 8, 42, 12];
var x;
// x = _.find(a, function(num) {
// return num > 5;
// });
x = _.filter(a, function(num) {
return num > 5;
@h2ospace
h2ospace / each.html
Created March 14, 2013 19:30
underscore: each, map
(function() {
// _.each([2, 5, 8], function(num) {
// console.log(num * 2);
// });
var x = _.map([2, 5, 8], function(num) {
return num * 2;
});
console.log(x);
})();
@h2ospace
h2ospace / keys.html
Created March 14, 2013 19:39
underscore: keys, values, invert, has, isstring
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Underscore</title>
<script src="underscore-min.js"></script>
</head>
<body>
@h2ospace
h2ospace / chain.html
Created March 14, 2013 19:44
underscore: chain, value
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Underscore</title>
<script src="underscore-min.js"></script>
</head>
<body>
@h2ospace
h2ospace / union.html
Created March 14, 2013 19:37
underscore: union, intersection, difference, uniq
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Underscore</title>
<script src="underscore-min.js"></script>
</head>
<body>
@h2ospace
h2ospace / range.html
Created March 14, 2013 19:42
underscore: range, random, escape, times
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Underscore</title>
<script src="underscore-min.js"></script>
</head>
<body>
@h2ospace
h2ospace / index.html
Created March 14, 2013 19:23
Underscore: shuffle
(function() {
var x;
x = _.shuffle([2, 8, 10, 3]);
//x = _([2, 8, 10, 3]).shuffle();
console.log(x);
})();
@h2ospace
h2ospace / groupby.html
Created March 14, 2013 19:34
underscore: groupBy, countBy, sortBy
(function() {
var a = [1, 2, 5, 8, 42, 12];
var x;
x = _.groupBy(a, function(num) {
return num % 3;
});
x = _.countBy(a, function(num) {
return num % 2 == 0 ? 'even' : 'odd';
@h2ospace
h2ospace / app.js
Created March 15, 2013 04:05
Backbone: model basic
(function() {
// Model
var Task = Backbone.Model.extend({
defaults: {
title: 'do something!',
completed: false
}
});
var task1 = new Task({
@h2ospace
h2ospace / app.js
Created March 15, 2013 06:35
Backbone: Model
(function() {
// Model
var Task = Backbone.Model.extend({
defaults: {
title: 'do something!',
completed: false
},
validate: function(attrs) {
if (_.isEmpty(attrs.title)) {
return "title must not be empty!";