-
Notifications
You must be signed in to change notification settings - Fork 0
/
js_closures.js
66 lines (50 loc) · 1.63 KB
/
js_closures.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/*
* This is a JavaScript Clojure example.
*/
var tag = function(name, attrs){
var attrs = attrs || {};
var getAttrs = function(new_attrs){
var _attrs = new_attrs || attrs;
var attr_str = "";
for(key in _attrs){
attr_str += " "+key+"='"+_attrs[key].replace("'", "\\'")+"'";
}
return attr_str || "";
}
return function(data){
var LT = "<",
GT = ">",
SL = "/";
if('object' === typeof data){
return tag(name, data);
}
return "<"+name+getAttrs(attrs)+">"+data+"</"+name+">";
}
}
var wrap = function(data){
for(arg in arguments){
var func = arguments[arg];
if('function' !== typeof func){
continue;
}
data = func(data);
};
return data;
}
//Create tag with predefined attribute
var a = tag("a", {url: "test'er"});
//Creta plain tag
var u = tag("u");
var i = tag("i");
var div = tag("div");
var div_container = tag("div", {id: "container"});
//Print tag with predefined attribute
console.log(a("Clojure is nice"));
//Creat a new tag with new attributes
console.log(a({url:"http://example.com", class:"blinking"})("Clojures are epic!"));
console.log(a("Clojure is nice 2"));
//Wrap content in the following tags, override the attributes in the a ta
var wrapped_content = wrap("How cool can it get?", a({href:'http://google.com'}), u, i, div({style:"background-color:red;"}), div_container);
console.log(wrapped_content);
var body = document.getElementsByTagName("html")[0];
body.innerHTML = wrapped_content;