8000 Add examples · HowProgrammingWorks/Prototype@e8612ea · GitHub
[go: up one dir, main page]

Skip to content

Commit e8612ea

Browse files
committed
Add examples
1 parent ccd981f commit e8612ea

16 files changed

+489
-2
lines changed

.eslintrc.yml

Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
1+
env:
2+
es6: true
3+
node: true
4+
extends: 'eslint:recommended'
5+
globals:
6+
api: false
7+
rules:
8+
indent:
9+
- error
10+
- 2
11+
- SwitchCase: 1
12+
VariableDeclarator:
13+
var: 2
14+
let: 2
15+
const: 3
16+
MemberExpression: 1
17+
linebreak-style:
18+
- error
19+
- unix
20+
quotes:
21+
- error
22+
- single
23+
semi:
24+
- error
25+
- always
26+
eqeqeq:
27+
- error
28+
- always
29+
no-loop-func 629A :
30+
- error
31+
strict:
32+
- error
33+
- global
34+
block-spacing:
35+
- error
36+
- always
37+
brace-style:
38+
- error
39+
- 1tbs
40+
- allowSingleLine: true
41+
camelcase:
42+
- error
43+
comma-style:
44+
- error
45+
- last
46+
comma-spacing:
47+
- error
48+
- before: false
49+
after: true
50+
eol-last:
51+
- error
52+
func-call-spacing:
53+
- error
54+
- never
55+
key-spacing:
56+
- error
57+
- beforeColon: false
58+
afterColon: true
59+
mode: minimum
60+
keyword-spacing:
61+
- error
62+
- before: true
63+
after: true
64+
overrides:
65+
function:
66+
after: false
67+
max-len:
68+
- error
69+
- code: 80
70+
ignoreUrls: true
71+
max-nested-callbacks:
72+
- error
73+
- max: 7
74+
new-cap:
75+
- error
76+
- newIsCap: true
77+
capIsNew: true
78+
properties: true
79+
new-parens:
80+
- error
81+
no-lonely-if:
82+
- error
83+
no-trailing-spaces:
84+
- error
85+
no-unneeded-ternary:
86+
- error
87+
no-whitespace-before-property:
88+
- error
89+
object-curly-spacing:
90+
- error
91+
- always
92+
operator-assignment:
93+
- error
94+
- always
95+
operator-linebreak:
96+
- error
97+
- after
98+
semi-spacing:
99+
- error
100+
- before: false
101+
after: true
102+
space-before-blocks:
103+
- error
104+
- always
105+
space-before-function-paren:
106+
- error
107+
- never
108+
space-in-parens:
109+
- error
110+
- never
111+
space-infix-ops:
112+
- error
113+
space-unary-ops:
114+
- error
115+
- words: true
116+
nonwords: false
117+
overrides:
118+
typeof: false
119+
no-unreachable:
120+
- error
121+
no-global-assign:
122+
- error
123+
no-self-compare:
124+
- error
125+
no-unmodified-loop-condition:
126+
- error
127+
no-constant-condition:
128+
- error
129+
- checkLoops: false
130+
no-console:
131+
- off
132+
no-useless-concat:
133+
- error
134+
no-useless-escape:
135+
- error
136+
no-shadow-restricted-names:
137+
- error
138+
no-use-before-define:
139+
- error
140+
- functions: false
141+
arrow-body-style:
142+
- error
143+
- as-needed
144+
arrow-spacing:
145+
- error
146+
no-confusing-arrow:
147+
- error
148+
- allowParens: true
149+
no-useless-computed-key:
150+
- error
151+
no-useless-rename:
152+
- error
153+
no-var:
154+
- error
155+
object-shorthand:
156+
- error
157+
- always
158+
prefer-arrow-callback:
159+
- error
160+
prefer-const:
161+
- error
162+
prefer-numeric-literals:
163+
- error
164+
prefer-rest-params:
165+
- error
166+
prefer-spread:
167+
- error
168+
rest-spread-spacing:
169+
- error
170+
- never
171+
template-curly-spacing:
172+
- error
173+
- never

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
node_modules
2+
*.log
3+
.DS_Store

JavaScript/1-object.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
'use strict';
2+
3+
const p1 = {
4+
x: 10,
5+
y: 20,
6+
move(x, y) {
7+
this.x += x;
8+
this.y += y;
9+
},
10+
toString() {
11+
return `[${this.x}, ${this.y}]`;
12+
}
13+
};
14+
15+
p1.move(-5, 10);
16+
17+
console.log(p1);
18+
console.log(p1.toString());
19+
console.log(p1 + '');

JavaScript/2-prototype.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
'use strict';
2+
3+
function Point(x, y) {
4+
this.x = x;
5+
this.y = y;
6+
}
7+
8+
Point.prototype.move = function(x, y) {
9+
this.x += x;
10+
this.y += y;
11+
};
12+
13+
Point.prototype.toString = function() {
14+
return `[${this.x}, ${this.y}]`;
15+
};
16+
17+
console.log('function prototype:', (function() {}).prototype);
18+
console.log('lambda prototype:', (() => {}).prototype);
19+
20+
console.log('Point prototype:', Point.prototype);
21+
console.log('move prototype:', Point.prototype.move.prototype);
22+
23+
const p1 = new Point(10, 20);
24+
p1.move(-5, 10);
25+
26+
console.log(p1);
27+
console.log(p1.toString());
28+
console.log(p1 + '');

JavaScript/3-class.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
'use strict';
2+
3+
class Point {
4+
constructor(x, y) {
5+
this.x = x;
6+
this.y = y;
7+
}
8+
9+
move(x, y) {
10+
this.x += x;
11+
this.y += y;
12+
}
13+
14+
toString() {< 10000 /span>
15+
return `[${this.x}, ${this.y}]`;
16+
}
17+
}
18+
19+
console.log(
20+
'Point prototype:',
21+
Point.prototype
22+
);
23+
console.log(
24+
'move prototype:',
25+
Point.prototype.move.prototype
26+
);
27+
console.log(
28+
'constructor prototype:',
29+
Point.constructor.prototype
30+
);
31+
console.log(
32+
'prototype constructor prototype:',
33+
Point.prototype.constructor.prototype
34+
);
35+
36+
const p1 = new Point(10, 20);
37+
p1.move(-5, 10);
38+
39+
console.log(p1);
40+
console.log(p1.toString());
41+
console.log(p1 + '');

JavaScript/4-closure.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
'use strict';
2+
3+
const point = (x, y) => {
4+
const p = {};
5+
6+
p.move = (dx, dy) => {
7+
x += dx;
8+
y += dy;
9+
};
10+
11+
p.toString = () => `[${x}, ${y}]`;
12+
13+
return p;
14+
};
15+
16+
const p1 = point(10, 20);
17+
p1.move(-5, 10);
18+
19+
console.log(p1);
20+
console.log(p1.toString());
21+
console.log(p1 + '');

JavaScript/5-bind.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
'use strict';
2+
3+
function move(x, y) {
4+
this.x += x;
5+
this.y += y;
6+
}
7+
8+
function toString() {
9+
return `[${this.x}, ${this.y}]`;
10+
}
11+
12+
const p1 = { x: 10, y: 20 };
13+
const p1move = move.bind(p1);
14+
const p1toString = toString.bind(p1);
15+
p1move(-5, 10);
16+
17+
console.log(p1);
18+
console.log(p1toString());
19+
console.log(p1 + '');

JavaScript/6-logger.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
'use strict';
2+
3+
const logger = level => {
4+
const color = logger.colors[level] || logger.colors.info;
5+
return s => {
6+
const date = new Date().toISOString();
7+
console.log(color + date + '\t' + s);
8+
};
9+
};
10+
11+
logger.colors = {
12+
warning: '\x1b[1;33m',
13+
error: '\x1b[0;31m',
14+
info: '\x1b[1;37m'
15+
};
16+
17+
const warning = logger('warning');
18+
const error = logger('error');
19+
const debug = logger('debug');
20+
const slow = logger('slow');
21+
22+
slow('I am slow logger');
23+
warning('Hello');
24+
error('World');
25+
debug('Bye!');

JavaScript/7-prototype.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
'use strict';
2+
3+
function Logger(level) {
4+
this.color = this.colors[level] || this.colors.info;
5+
}
6+
7+
Logger.prototype.colors = {
8+
warning: '\x1b[1;33m',
9+
error: '\x1b[0;31m',
10+
info: '\x1b[1;37m'
11+
};
12+
13+
Logger.prototype.log = function(s) {
14+
const date = new Date().toISOString();
15+
console.log(this.color + date + '\t' + s);
16+
};
17+
18+
const warning = new Logger('warning');
19+
const error = new Logger('error');
20+
const debug = new Logger('debug');
21+
const slow = new Logger('slow');
22+
23+
slow.log('I am slow logger');
24+
warning.log('Hello');
25+
error.log('World');
26+
debug.log('Bye!');

JavaScript/8-class.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
'use strict';
2+
3+
class Logger {
4+
constructor(level) {
5+
this.color = this.colors[level] || this.colors.info;
6+
}
7+
8+
log(s) {
9+
const date = new Date().toISOString();
10+
console.log(this.color + date + '\t' + s);
11+
}
12+
}
13+
14+
Logger.prototype.colors = {
15+
warning: '\x1b[1;33m',
16+
error: '\x1b[0;31m',
17+
info: '\x1b[1;37m'
18+
};
19+
20+
const warning = new Logger('warning');
21+
const error = new Logger('error');
22+
const debug = new Logger('debug');
23+
const slow = new Logger('slow');
24+
25+
slow.log('I am slow logger');
26+
warning.log('Hello');
27+
error.log('World');
28+
debug.log('Bye!');

0 commit comments

Comments
 (0)
0