Skip to content

Commit 54b8745

Browse files
author
David Brady
committed
Created Metaprogramming chapter, added metaprogramming/detecting-and-replacing-functions
1 parent 1b56d99 commit 54b8745

File tree

4 files changed

+48
-1
lines changed

4 files changed

+48
-1
lines changed

chapters/index.textile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ chapters:
99
- Dates and Times
1010
- Math
1111
- Functions
12+
- Metaprogramming
1213
- jQuery
1314
- Regular Expressions
1415
- AJAX
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
---
2+
layout: recipe
3+
title: Detecting and Creating Missing Functions
4+
chapter: Metaprogramming
5+
---
6+
7+
h2. Problem
8+
9+
You want to detect if a function exists and create it if it does not (such as an ECMAScript 5 function in Internet Explorer 8).
10+
11+
h2. Solution
12+
13+
Use :: to detect the function, and assign to it if it does not exist.
14+
15+
{% highlight coffeescript %}
16+
unless Array::filter
17+
Array::filter = (callback) ->
18+
element for element in this when callback(element)
19+
20+
array = [1..10]
21+
22+
array.filter (x) -> x > 5
23+
# => [6,7,8,9,10]
24+
{% endhighlight %}
25+
26+
h2. Discussion
27+
28+
Objects in JavaScript (and thus, in CoffeeScript) have a prototype member that defines what member functions should be available on all objects based on that prototype. In CoffeeScript, you can access the prototype directly via the :: operator.
29+

chapters/objects/extending-classes.textile renamed to chapters/metaprogramming/extending-classes.textile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
layout: recipe
33
title: Extending Classes
4-
chapter: Objects
4+
chapter: Metaprogramming
55
---
66

77
h2. Problem
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
---
2+
layout: chapter
3+
title: Metaprogramming
4+
chapter: Metaprogramming
5+
---
6+
7+
{% capture url %}/chapters/{{ page.chapter | replace: ' ', '_' | downcase }}{% endcapture %}
8+
{% capture indexurl %}{{ url }}/index.html{% endcapture %}
9+
10+
{% for page in site.pages %}
11+
{% if page.url contains url %}
12+
{% unless page.url == indexurl %}
13+
* <a href="{{ page.url | replace: '.html', '' }}">{{ page.title }}</a>
14+
{% endunless %}
15+
{% endif %}
16+
{% endfor %}
17+

0 commit comments

Comments
 (0)