Object.defineProperty()ã«ã¤ãã¦èª¿ã¹ã
TypeScriptæ¬ã®5ç« ã§ã¯ã³ã³ãã¤ã«å¾ã®JSãã¡ã¤ã«ãèªããããã«JavaScriptã®ä»æ§ã«ã¤ãã¦è§£èª¬ããã¦ãã¾ãã
Object.defineProperty()
ã¨ããã¡ã½ããããåç¥ã§ããããã
ãã®ã¡ã½ããã¯ä¸»ã«æ¸ãæãä¸å¯è½ãªãªãã¸ã§ã¯ãããããã£ãå®ç¾©ãããã使ãã¾ãã
ãããä»ã¾ã§ç¥ãã¾ããã§ããã»ã»
ã£ã¦ãã¨ã§ããããã¸ãããµã¤æ¬ãè¦ãªãã復ç¿ãã¦ã¿ã¾ãã
Object.defineProperty()ã¨ã¯
ECMAScript5ãããªãã¸ã§ã¯ãã®ããããã£ã«ãå¤ä»¥å¤ã«ããè¨å®ãä»ä¸ãããã¨ãã§ããããã«ãªã£ã¦ãã¾ãã
è¨å®ã¨ããã®ã¯ã
- æ¸ãè¾¼ã¿å¯è½ã
- åæå¯è½ã
- åå®ç¾©å¯è½ã
ã¨ããæ å ±ã«ãªãã¾ãã
Chromeã®ã³ã³ã½ã¼ã«ã«ä»¥ä¸ã®ã³ã¼ããè²¼ãä»ãã¦å®è¡ããã¨ããã®ãããªçµæãå¾ããã¾ãã
// ãããè²¼ãä»ãã㨠Object.getOwnPropertyDescriptor({x:1}, 'x') // ããããçµæãå¾ããã Object {value: 1, writable: true, enumerable: true, configurable: true}
valueãå¤ã§ããã以å¤ãxã«å¯¾ããè¨å®ã«ãªãã¾ãã
åã« var a = {x:1};
ã¨å®ç¾©ããå ´åã¯ã
- writable: true (æ¸ãè¾¼ã¿å¯)
- enumerable: true (åæå¯)
- configurable: true (åå®ç¾©å¯)
ã¨ãã¦å®ç¾©ããããã¨ã«ãªãã¾ãã
writable
ååã®éããtrueã ã¨ä¸æ¸ãã許å¯ãã¾ãã
falseã ã¨ããããã£ã«å¤ãå代å
¥ãã¦ãç¡è¦ããã¾ãã
ï¼â» 'use strict'
ã¢ã¼ãã ã¨ãTypeErrorãã¹ãã¼ãããï¼
使ãã©ããã¯å¤ãå¤æ´ãããããªãå ´åã§ããã
enumerable
trueã ã¨åæããã¾ãã
åæã¨ããã®ã¯æ¬¡ã®ãããªã³ã¼ãã®å ´åã§ãã
// ãããªãªãã¸ã§ã¯ãããã£ã¦ var o = { a : 1, b : 2, c : 3 }; // Object.keysã§ããããã£ãåæããã var keys = Object.keys(o); conosle.log(keys); // => ['a', 'b', 'c'] // for in ã§åæããã¨ã var i; for (i in o) { console.log(i); // => 'a', 'b', 'c' }
enumerable: false
ã ã¨ãåæãããªããªãã¾ãã
ï¼åæãããªããªãã ãã§ãããããã£ã«ã¢ã¯ã»ã¹ãã¦å¤ãåå¾ãããã¨ã¯ã§ãã¾ãï¼
configurable
configurableã¯ããããã£ã®è¨å®ãå¤æ´å¯è½ãã©ãããæå®ãããã®ã§ã
å®ã¯writable: false
ã¨è¨å®ãã¦ããconfigurable: true
ã ã¨ãå¤ãæ¸ãæãããã¨ãå¯è½ã§ãã
ããããã£ã®å¤æ´ãè¡ãã«ã¯Object.defineProperty()
ã使ãã¾ãã
Object.defineProperty()ã®ä½¿ãæ¹
第ä¸å¼æ°ã«ãªãã¸ã§ã¯ãã第äºå¼æ°ã«ãªãã¸ã§ã¯ãã®ããããã£åã第ä¸å¼æ°ã«å¤ã¨è¨å®ãã¾ã¨ãããªãã¸ã§ã¯ãã渡ãã¾ãã
ãã¨ãã°ãæ¸ãè¾¼ã¿ä¸å¯ã®ããããã£ããªãã¸ã§ã¯ãã«è¿½å ããã¨ãã¯ãããªãããã
// æ¸ãè¾¼ã¿ä¸å¯ã®ãããã㣠var obj = {}; Object.defineProperty(obj, 'x', { value : 1, writable : false, // æ¸ãè¾¼ã¿ä¸å¯ enumerable : true, configurable : true }); console.log(obj.x); // => 1 obj.x = 99; console.log(obj.x); // => 1
configure: true
ãªã®ã§ãObject.defineProperty()
ã使ãè¨å®ãå¤æ´ãããã¨ãã§ãã¾ãã
// æ¸ãè¾¼ã¿ä¸å¯ã®ãããã㣠var obj = {}; Object.defineProperty(obj, 'x', { value : 1, writable : false, // æ¸ãè¾¼ã¿ä¸å¯ enumerable : true, configurable : true }); // æ¸ãè¾¼ã¿å¯è½ã«å¤æ´ Object.defineProperty(obj, 'x', { writable : true }); console.log(obj.x); // => 1 obj.x = 99; console.log(obj.x); // => 99
ã¾ããconfigurable: false
ã®ããããã£ã«å¯¾ãã¦ãObject.defineProperty()
ã使ãè¨å®ãå¤æ´ãããã¨ããã¨TypeError
ä¾å¤ãã¹ãã¼ãããããã§ãã
Object.defineProperties()
ä¸åº¦ã«è¤æ°ã®ããããã£ãä½æããããè¨å®ãå¤æ´ããããããå ´åã¯Object.defineProperties()
ã使ãã¾ãã
使ãæ¹
第ä¸å¼æ°ã«å¯¾è±¡ã¨ãªããªãã¸ã§ã¯ãã第äºå¼æ°ã«ä½æï¼ã¾ãã¯å¤æ´ï¼ããããããã£åã¨ãå¤ã¨è¨å®ãããªããªãã¸ã§ã¯ãã渡ãã¾ãã
// ããããã£ãä¸åº¦ã«ä½æã»è¨å®ãã var obj = Object.defineProperties({}, { x : { value: 1, writable: true, enumerable: true, configurable: true }, y : { value: 2, writable: true, enumerable: true, configurable: true } });
æ¡å¼µå¯å±æ§ï¼extensibleï¼
æ¡å¼µã¨ããã®ã¯ããªãã¸ã§ã¯ãã«æ°ããããããã£ã追å ãããã¨ãæãã¾ãã
ãã¨ãã°ããããªæ¸ãæ¹ãæ¡å¼µã§ãã
var o1 = {}; o1.x = 1; // xããããã£ã追å
æ¡å¼µä¸å¯ã«ãããã¨ãã§ããããã§ãObject.preventExtensions()
ã使ãã¾ãã
var obj = {}; Object.preventExtensions(obj); // æ¡å¼µä¸å¯ã«ãã obj.x = 10; console.log(obj); // => {}
æ¡å¼µãã§ããªãã ãã§ãããããã£ãåé¤ãããã¨ã¯å¯è½ã§ãã
var obj = { x: 1, y: 2 }; Object.preventExtensions(obj); // æ¡å¼µä¸å¯ã«ãã delete obj.x; // xãåé¤ console.log(obj); // => { y: 2 }
æ¡å¼µãä¸å¯ã«ãã¦ããªãã¸ã§ã¯ãã®ããããã£ã®åé¤ãç¦æ¢ããã«ã¯Object.seal()
ã使ãã¾ãã
var obj = { x: 1, y: 2 }; Object.seal(obj); // æ¡å¼µä¸å¯ãåå®ç¾©ä¸å¯ã«ãã obj.x = 100; obj.z = 99; delete obj.y; console.log(obj); // => { x: 100, y: 2 }
Object.seal()
ã§ã¯ããããã£ã®åé¤ã¯ç¦æ¢ã§ãã¾ããããobj.x
ãããã©ã«ãã§writable: true
ãªã®ã§å代å
¥ãå¯è½ã«ãªã£ã¦ãã¾ãã
ãã£ã¨å³ãããªãã¸ã§ã¯ãã®å¤æ´ãç¦æ¢ããã«ã¯Object.freeze()
ã使ãã¾ãã
var obj = { x: 1, y: 2 }; Object.freeze(obj); // æ¡å¼µä¸å¯ãåå®ç¾©ä¸å¯ãæ¸ãè¾¼ã¿ä¸å¯ã«ãã obj.x = 100; obj.z = 99; delete obj.y; console.log(obj); // => { x: 1, y: 2 }
ã¡ãªã¿ã«Object.seal()
ãObject.freeze()
ãããªãã¸ã§ã¯ãã®ããããã£å
¨ã¦ãåå®ç¾©ä¸å¯ã«è¨å®ããã®ã§ããã¨ããObject.defineProperty()
ã§å¤æ´ãããã¨ã¯ã§ããªãã®ã§æ³¨æã
ããããç´¹ä»ãã¾ãããä½ãè¨ãããã£ããã¨ããã¨ã
JavaScriptã¨ããã®ã¯è¨èªä»æ§ä¸ã容æã«å¤ãå¤æ´ããããå£ããããããã¨ãã§ããã¨ããèªèã ã£ãã®ã§ãããES5ãã追å ããããããã®ä»è¾¼ã¿ã使ããã¨ã§å°ãã¯å¼·åºãªã³ã¼ããããããã§ãã
ã¡ãã£ã¨è¨å®ã«é¢åèããããã®ã§ããªãã¸ã§ã¯ãã®çæãæ½è±¡åãã¦ä½¿ãã®ãè¯ãããã
ã©ã¤ãã©ãªãä½ã人ã¯æ°ã«ããã»ããè¯ãããã§ãã
ãã¨èª¬æããã¨ãã«ãå¤ã¨è¨å®ãããªããªãã¸ã§ã¯ãã¨ãè¨ã£ã¦ã¾ãããã ã¡ããã¨ååãããããã®è¨äºã§æãã¦ããã®ã¯ãã¼ã¿ãã£ã¹ã¯ãªãã¿ã®ãã¨ã§ãã
get, setãæå®ãããããã¢ã¯ã»ãµãã£ã¹ã¯ãªãã¿ã¨ããã®ãããã®ã§ããã
ä»åã¯è§¦ãã¾ããã§ããã
詳ãã説æããã¡ãã®è¨äºã§ã¾ã¨ãããã¦ããã®ã§ãç´¹ä»ã
ES5, Property Descriptor解説 | æã欹ã¦ã¦è´ã
ãããã«
TypeScriptæ¬ãèªãã§ããããµã¤æ¬èªãã§ããã
ããES6ãåºãã¨ãããªã®ã«ES5ã«ã¤ãã¦ç¥ããªããã¨ããããããããããã
ã¾ã TypeScriptæ¬ãå ¨é¨èªã¿çµãã¦ãªãã®ã§å¼ãç¶ãèªãã§ããã¾ãã