Wissen
Werkzeuge
Wiederholung
Know the pitfalls
Forgetting var
var foo = function() {
bar = 1;
};
To Truthy or to Falsy. That is the only question!
var el = document.getElementById('does_not_exist');
if(el == false) {
alert("shouldn't we see this message?!");
}
works on my machine!
var foo = {
bar: "bar",
baz: "baz",
};
señor developers wear mustaches
var foo = function() {
return
{
x : "looks like C# now!"
};
}
they don't exist
var x = [];
x['foo'] = "bar";
who cares about the reason?
var foo = function() {
try {
doCrazyStuff;
} catch (e) {
return false;
}
return true;
};
declare upfront all variables
var foo = "global";
var bar = function() {
alert(foo);
var foo = "local";
alert(foo);
};
... and the job is done
function poorMansJsonParser(text) {
return eval("(" + text + ")");
}
var text = ' { "hello" : "world" } ';
var json = poorMansJsonParser(text);
Never ever!
var text = ' function() { alert("hacked!"); })( ';
the mother of all antipatterns
function foo() {
return "bar";
}
console.log(this['foo']());
Every time you clutter the global namespace,
somewhere in the world a helpless kitten dies!
Pretty Code
reduce, minimize, delete or kill them
(function() { "wtf?" })();
a functional language wants functions!
switch (something) {
case 1:
doFirst();
break;
case 2:
doSecond();
break;
case 3:
doThird();
break;
}
avoid the switch-case syndrome
var methods = {
1: doFirst,
2: doSecond,
3: doThird
};
if (methods[something]) {
methods[something]();
}
var myRevealingModule = function () {
var _name = "Johannes";
function greetings() {
console.log("Hello " + _name);
}
function setName(name) {
_name = name;
}
return {
setName: setName,
greetings: greetings
};
}();
use AMD (require.js)
define('test', ['jquery'], function() {
return {
saySomething : function() { alert("hello!"); }
}
});
require(['test'], function(t) {
t.saySomething();
});
Publish/Subscribe Pattern
var $events = $({});
$events.bind('somethingHappens', function() {
alert("Something happened!");
});
$events.trigger('somethingHappens');
TDD with Jasmine
<!DOCTYPE html>
<html>
<head>
<title>Jasmine Spec Runner</title>
<link rel="stylesheet" href="lib/jasmine-1.3.1/jasmine.css" />
<script src="lib/jasmine-1.3.1/jasmine.js"></script>
<script src="lib/jasmine-1.3.1/jasmine-html.js"></script>
<!-- include source files here... -->
<script src="src/Player.js"></script>
<script src="src/Song.js"></script>
<!-- include spec files here... -->
<script src="spec/SpecHelper.js"></script>
<script src="spec/PlayerSpec.js"></script>
<script>
(function () {
var htmlReporter = new jasmine.HtmlReporter();
var jasmineEnv = jasmine.getEnv();
jasmineEnv.addReporter(htmlReporter);
jasmineEnv.specFilter = function (spec) {
return htmlReporter.specFilter(spec);
};
var currentWindowOnload = window.onload;
window.onload = function () {
if (currentWindowOnload) { currentWindowOnload(); }
jasmineEnv.execute();
};
})();
</script>
</head>
<body>
</body>
</html>
var helloWorld = function() {
return "Hello World!";
};
describe('helloWorld', function() {
it('says hello', function() {
expect(helloWorld()).toEqual("Hello World!");
});
});
jasmine.getEnv().execute();
hint: press F12 and paste this code!
If I would have had time...
see Growing Object-Oriented Software, Guided by Tests, page 6
see Working Effectively with Legacy Code, page 62 or many other
describe("saveFormat", function () {
var original = '{0} - {1} - {2}';
it("should replace placeholders", function () {
var expected = 'A - B - C';
var formated = saveFormat(original, 'A', 'B', 'C');
expect(formated).toEqual(expected);
});
it("should encode injected content", function () {
var expected = 'A - <b>TEST</b> - C';
var formated = saveFormat(original, 'A', '<b>TEST</b>', 'C');
expect(formated).toEqual(expected);
});
});
var saveFormat = function() {
return "boo!";
};
jasmine.getEnv().execute();
var saveFormat = function(txt) {
$(arguments).each(function (i, item) {
if (i > 0) {
item = ($('<div/>').text(item).html());
txt = txt.replace("{" + (i - 1) + "}", item);
}
});
return txt;
};
jasmine.getEnv().execute();
function htmlEncode(input) {
return ($('<div/>').text(input).html());
}
var saveFormat = function(txt) {
$.each(arguments, function (i, item) {
if (i > 0) {
item = htmlEncode(item);
txt = txt.replace("{" + (i - 1) + "}", item);
}
});
return txt;
};
jasmine.getEnv().execute();
function htmlEncode(input) {
return ($('<div/>').text(input).html());
}
var saveFormat = function() {
var args = Array.prototype.slice.call(arguments);
var txt = args.shift();
$.each(args, function (i, item) {
item = htmlEncode(item);
txt = txt.replace("{" + i + "}", item);
});
return txt;
};
jasmine.getEnv().execute();
Created by Johannes Hoppe | Print PDF | GitHub