@@ -196,16 +196,98 @@ console.log(end-start);
196
196
197
197
## Immutability
198
198
199
+ * pattern
200
+ * [ Immutable object - Wikipedia] ( https://en.wikipedia.org/wiki/Immutable_object#JavaScript )
201
+ * in book
202
+ * 변수가 단지 한 번만 할당될 수 있다는 것(함수형 프로그래밍 토대 중 하나)
203
+ * ES6 - const
204
+ * [ const - JavaScript _ MDN] ( https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/const )
205
+ * Object.freeze
206
+ * 책에서 사용을 권장함.
207
+ * Object.create - { writable: false }
208
+
209
+ ES6 - const
210
+
211
+ ``` javascript
212
+ const number = 42 ;
213
+
214
+ try {
215
+ number = 99 ;
216
+ } catch (err) {
217
+ console .log (err);
218
+ // expected output: TypeError: invalid assignment to const `number'
219
+ // Note - error messages will vary depending on browser
220
+ }
221
+
222
+ console .log (number);
223
+ // expected output: 42
224
+ ```
225
+
226
+ (not strict mode)Object.freeze
227
+
228
+ 오류를 알려주지 않고 실패하는 문제가 있음.(조용히 실패)
229
+
199
230
``` javascript
200
- " use strict" ;
201
231
var consts = Object .freeze ({ pi: 3.141 });
202
232
consts .pi = 7 ;
233
+ console .log (consts); // {pi: 3.141}
234
+ ```
235
+
236
+ (strict mode)Object.freeze
237
+
238
+ 예외가 발생
239
+
240
+ ``` javascript
241
+ " use strict" ;
242
+ var consts = Object .freeze ({ pi: 3.141 });
243
+ consts .pi = 7 ; // Uncaught TypeError: Cannot assign to read only property 'pi' of object '#<Object>'
244
+ ```
245
+
246
+ (not strict mode)Object.create
247
+
248
+ ``` javascript
249
+ var t = Object .create (Object .prototype , {
250
+ value: {
251
+ writable: false ,
252
+ value: 10
253
+ }
254
+ });
255
+ t .value = 7 ;
256
+ console .log (t .value ); // 10
257
+ ```
258
+
259
+ (strict mode)Object.create
260
+
261
+ ``` javascript
262
+ " use strict" ;
263
+ var t = Object .create (Object .prototype , {
264
+ value: {
265
+ writable: false ,
266
+ value: 10
267
+ }
268
+ });
269
+ t .value = 7 ; // Uncaught TypeError: Cannot assign to read only property 'value' of object '#<Object>'
203
270
```
204
271
205
272
** [ Back to top] ( #table-of-contents ) **
206
273
207
274
## Lazy instantiation
208
275
276
+ * pattern
277
+ * [ Lazy initialization - Wikipedia] ( https://en.wikipedia.org/wiki/Lazy_initialization#JavaScript )
278
+ * 객체 생성, 값 계산을 지연시키는 전략
279
+ * 팩토리 매서드와 함께 종종 쓰임(lazy factory)
280
+ * in book
281
+ * 예. 커피, 빵
282
+ * ** 주문이 들어오면 생성**
283
+ * like 지연 초기화(lazy initialization)
284
+ * 객체가 생성 비용이 많이 들고, 정말 필요한지 확신이 없는 경우, 객체의 생성을 나중으로 연기할 수 있음.
285
+ * 비동기 프로그래밍을 단순화
286
+ * ** promise**
287
+ * 상태, 결과를 포함
288
+ * 결과를 Lazy instantiation 로 볼 수 있음.
289
+ * ** 사용하지 않는 고가의 객체를 생성하는 데 소비되는 상당항 시간을 절약**
290
+
209
291
``` javascript
210
292
var Westeros;
211
293
(function (Westeros ) {
@@ -218,6 +300,7 @@ var Westeros;
218
300
this .requiredBreads .push (breadType);
219
301
};
220
302
303
+ // lazy instantiation
221
304
Bakery .prototype .pickUpBread = function (breadType ) {
222
305
console .log (" Picup of bread " + breadType + " requested" );
223
306
if (! this .breads ) {
@@ -229,6 +312,7 @@ var Westeros;
229
312
}
230
313
};
231
314
315
+ // called in this.pickUpBread()
232
316
Bakery .prototype .createBreads = function () {
233
317
this .breads = [];
234
318
for (var i = 0 ; i < this .requiredBreads .length ; i++ ) {
0 commit comments