1+ <!doctype html>
2+ < html >
3+ < head >
4+ < title > JavaScript Patterns</ title >
5+ < meta http-equiv ="content-type " content ="text/html; charset=UTF-8 ">
6+ </ head >
7+ < body >
8+ < script >
9+ /***
10+ var jsp = {};
11+ jsp.dom = {};
12+ jsp.dom.Text = function() {
13+ this.insert = function(where) {
14+ var txt = document.createTextNode(this.url);
15+ where.appendChild(txt);
16+ };
17+ };
18+ jsp.dom.Link = function() {
19+ this.insert = function(where) {
20+ var link = document.createElement('a');
21+ link.href = this.url;
22+ link.appendChild(document.createTextNode(this.url));
23+ where.appendChild(link);
24+ };
25+ };
26+ jsp.dom.Image = function() {
27+ this.insert = function(where) {
28+ var im = document.createElement('img');
29+ im.src = this.url;
30+ where.appendChild(im);
31+ };
32+ };
33+ jsp.dom.factory = function(type) {
34+ return new jsp.dom[type];
35+ }
36+
37+ var o = jsp.dom.factory('Link');
38+ o.url = 'http://google.com'
39+ o.insert(document.body);
40+
41+ var taskManager = {};
42+
43+ taskManager.update = function() {
44+ console.log("update");
45+ }
46+
47+ taskManager.read = function() {
48+ console.log("read");
49+ }
50+
51+ var type = "update";
52+ var task;
53+
54+ if (type === 'update') {
55+ task = new taskManager.update();
56+ }
57+
58+ if (type === 'read') {
59+ task = new taskManager.read();
60+ }
61+
62+ taskManager.factory = function (typeType) {
63+ return new taskManager[typeType];
64+ }
65+
66+ task = new taskManager[type];
67+
68+ /*** Built-in Object Factory ***/
69+ var o = new Object ( ) ,
70+ n = new Object ( 1 ) ,
71+ s = Object ( '1' ) ,
72+ b = Object ( true ) ;
73+
74+ // test
75+ o . constructor === Object ; // true
76+ n . constructor === Number ; // true
77+ s . constructor === String ; // true
78+ b . constructor === Boolean ; // true
79+
80+ </ script >
81+ </ body >
82+ </ html >
0 commit comments