ECMAScript5æä»£ã®ã¯ã©ã¹å®ç¾©
Firefox4ã§ES5ã®ããããã£å®ç¾©ããµãã¼ãããããããªã®ã§ï¼å®éã«ä½¿ãã¨ãã«åãã¦è¦ãæ¸ããã¦ã¿ãã
ããã¾ã§ãããªæãã§æ¸ãã¦ãJavaScriptã®ã¯ã©ã¹å®ç¾©ã
// ã¯ã©ã¹ const Hoge = function(prop) { this._prop = prop; }; Hoge.prototype = { _prop: null, get prop() this._prop, set prop(value) this._prop = value, method: function() {} }; let hoge = new Hoge(); // ã·ã³ã°ã«ãã³ const Fuga = { _prop: null, get prop() this._prop, set prop(value) this._prop = value, method: function() {} };
ä»å¾ã¯ããæ¸ãã¨è¯ãããï¼
// ã¯ã©ã¹ const Hoge = function(prop) { this._prop = prop; }; Object.defineProperties(Hoge.prototype, { _prop: { value: null, enumerable: false }, prop: { get: function() this._prop, set: function(value) this._prop = value }, method: { value: function() {}, writable: false, configurable: false } }); Hoge.preventExtensions(); let hoge = new Hoge(); hoge.preventExtensions(); // ã·ã³ã°ã«ãã³ const Fuga = Object.create({}, { _prop: { value: null, enumerable: false }, prop: { get: function() this._prop, set: function(value) this._prop = value }, method: { value: function() {}, writable: false, configurable: false } }); Fuga.preventExtensions();
宿°ããããã£ã¨ãï¼ããããã£ã®è¿½å ãã§ããªããªãã¸ã§ã¯ããä½ããããã«ãªãã®ã¯ããããããããã©ï¼å®éã«æ¸ãã¦ã¿ãã¨çµæ§ããã©ãããããã©ããããã£åã®è¨è¿°ãã¹ãæ¸ããã®ã«ã¯ãããè²¢ç®ãã¦ããããã ããé å¼µã£ã¦æ¸ãã¦ãããã¨æãã