r.jsã§ãã«ãããéã«è¨å®ã¨ãã¦ä½¿ãbuild.jsãåçã«ãããããã«é¨ååããã
r.js
RequireJSç¨ã®moduleããã«ãããéã«ã¯r.jsã使ãã¾ãã
build.js
ãã®æã«ãã«ãè¨å®ã®è¨è¿°ã«ä½¿ç¨ããã®ãbuild.jsã§こんな感じã§æ¸ãã¾ãã
ç°¡åãªä¾ã ã¨ãããªã®
({ baseUrl:'./src', dir : './build', paths:{ jquery:"../lib/jquery-1.8.2", text:'../lib/plugins/text', underscore:'../lib/underscore-1.3.3' } })
åçãªbuild.js
build.jsã¯JSONã§ã¯ãªãã¦JSã§ãã
JSãªã®ã§å¦çãæ¸ããã¨ãåºæ¥ã¾ãã
ä¾ãã°é¢æ°ãã®å ´å®è¡ãå«ãä¸è¨ã®build.jsã¯ãä¸è¨ã®ãã®ã¨åãè¨å®ã表ãã¾ãã
({ baseUrl:'./src', dir : './build', paths:(function () { var paths = { jquery:"../lib/jquery-1.8.2", text:'../lib/plugins/text', underscore:'../lib/underscore-1.3.3' }; return paths; })() })
ã¡ãªã¿ã«å
¨ä½ãé¢æ°ãã®å ´å®è¡ã§å
ããã¨ã¯åºæ¥ã¾ããã
以ä¸ã®ã³ã¼ãã¯build.jsã¨ãã¦ã¯ä½¿ãã¾ããã
(function () { return ({ baseUrl:'./src', dir : './build', paths:{ jquery:"../lib/jquery-1.8.2", text:'../lib/plugins/text', underscore:'../lib/underscore-1.3.3' } }); })();
ãã¦ãç¾ç¶ã§ã¯build.jsã®ä¸ã«å¦çãæ¸ããããã«ãªã£ããã®ã®ãä½ã以ã¦è¨å®ãå¤æ´ããããåãããªãã®ã§ãåçã¨ããã»ã©ã§ã¯ãªãã¦ãã¾ãå¬ããããã¾ããã
r.jsã¯Node.jsã§å®è¡ããã¦ããã®ã§ãprocess.argvã§ã³ãã³ãã©ã¤ã³å¼æ°ãåç
§ãããã¨ãåºæ¥ã¾ãã
r.js -o build.js optimize='none' os='ios5'
({ baseUrl:'./src', dir : './build', paths:(function () { var paths = { jquery:"../lib/jquery-1.8.2", text:'../lib/plugins/text', underscore:'../lib/underscore-1.3.3' }; var os = (process.argv.filter(function (arg) { return arg.match(/^os=/); })[0] || '').replace(/^os=/,''); if (os === 'ios5') { // ä½ããã®å¦ç } return paths; })() })
ããã§ã³ãã³ãã©ã¤ã³ãªãã·ã§ã³ã«ãã£ã¦ãã«ãæã®è¨å®ãç´°ããå¤ãããã¨ãåºæ¥ãããã«ãªãã¾ããã
build.jsã®Node.js moduleå
ç¹°ãè¿ãã¾ããå
¨ä½ãé¢æ°ãã®å ´å®è¡ã§å
ããã¨ã¯åºæ¥ã¾ããã
objectã®valueããé¢æ°ãã®å ´å®è¡ã§å
ãã¾ããã
ã¤ã¾ããå¦çãé¢æ°ã¨ãã¦å
±éåãããã¦ãããããç½®ãã¦ããå ´æãããã¾ããã
ä¾ãã°åé
ç®ã§os optionã«ããæ¯ãåããè¡ããããªããåé
ç®ã®valueã®é¢æ°ãã®å ´å®è¡ã®ä¸ã«ä»¥ä¸ãå
¥ããå¿
è¦ãããã¾ãã
var os = (process.argv.filter(function (arg) { return arg.match(/^os=/); })[0] || '').replace(/^os=/,'');
ããã§ã¯ã¡ãã£ã¨ãããããã¨ãå¢ããã°ããã«è¥å¤§åãã¦ãã¾ãã¾ãã
ãã¦ãç¹°ãè¿ãã¾ããr.jsã¯Node.jsã§å®è¡ããã¦ãã¾ããå
±éåãããå¦çã¯Node.jsã®moduleã«ãã¦ãã¾ãã¾ãããã
以ä¸ã®ãããªå
容ã®options.jsãä½ãã¾ãã
var os = (process.argv.filter(function (arg) { return arg.match(/^os=/); })[0] || '').replace(/^os=/,''); module.exports = function options(){ return { os:os } };
ããã使ãéã®æ³¨æç¹ã¯äºã¤ããã¾ãã
- Node.jsã®moduleèªã¿è¾¼ã¿é¢æ°requireã¯r.jsã§ã¯require.jsã®ããã§ä¸æ¸ãããã¦ãã¾ãã
- nodeRequireãå ã ã®requireé¢æ°ãªã®ã§ããã使ãã¾ãã
- Node.jsã§å®è¡ããã¦ããã®ã¯r.jsãªã®ã§ãbuild.jsããã®ç¸å¯¾pathã§moduleãèªã¿è¾¼ããã¨ãã¦ãç¡é§ã§ãã
- process.env.PWDã使ã£ã¦r.jsãå®è¡ããã¨ãã®ã«ã¬ã³ããã£ã¬ã¯ããª(build.jsã¯ããã«ããåæ)ãåç §ãã¾ãããã
ãããªæãã§ããããbuild.jsãåçã«ãã¦ãããã¨ãåºæ¥ã¾ãã
è¤æ°ç°å¢åããã«ãããããã¨ãè¡ã£ãå ´åã«æãã¾ããã
({ baseUrl:'./src', dir : './build', paths:(function () { var paths = { jquery:"../lib/jquery-1.8.2", text:'../lib/plugins/text', underscore:'../lib/underscore-1.3.3' }; var options = nodeRequire(process.env.PWD+'/build-tools/options')(); var os = options['os']; if (os === 'ios5') { // ä½ããã®å¦ç } return paths; })() })
Require JSã§è¨å®ãmoduleå¤ã«åºãã¦ãããããã«ãæã«å梱ãã
ã¢ã¸ã¥ã¼ã«å¥ã®è¨å®ãèªã¿è¾¼ã
RequireJSã§ã¯ã¢ã¸ã¥ã¼ã«ã®ååã¨ãã¦"module"ãäºç´ããã¦ãã¦ãã¢ã¸ã¥ã¼ã«å ã§moduleã¢ã¸ã¥ã¼ã«ãåå¾ããã¨ãã®ã¢ã¸ã¥ã¼ã«ã®æ å ±ãåç §ãããã¨ãåºæ¥ã¾ãã
define('foo',['module'], function (module) { });
RequireJS API
config methodã§è¨å®ã§ããé
ç®ã®ä¸ã®configã«ã¢ã¸ã¥ã¼ã«åãkeyä½ããã®ãªãã¸ã§ã¯ããvalueã¨ããè¾æ¸ãè¨å®ãã¦ããã¨ã
moduleã¢ã¸ã¥ã¼ã«ã®config methodã§ãã®ãªãã¸ã§ã¯ããåå¾ãããã¨ãåºæ¥ã¾ãã
ä»åã¯module moduleã¨ãconfig configã¨ãè¦ã¦ã¦æ··ä¹±ããã®ãå¤ãã§ããã
ãããªè¨å®ãããã¨
requirejs.config({ config: { 'baz': { color: 'blue' } } });
ãããªãµãã«åãã¾ãã
define('baz',['module'], function (module) { //Will be the value 'blue' var color = module.config().color; });
ãããã£ãè¨å®ãr.jsã§ã®ãã«ãæã«å梱ãã¦requirejs.config/require.configã§ã®è¨å®ã¨ä¸ç·ã«ä½¿ãããããã©ã«ãå¤ã®ãããªå½¢ã«ãããã¨ã¯åºæ¥ãªãããã§ãã
è¨å®è¾¼ã¿ã®ãã«ãã¨ããã®ã¯åºæ¥ãªãããã§ãã
ãã«ãæã«è¨å®ãå梱ãã
å梱åºæ¥ãªãè¨å®ãã©ããã£ã¦å梱ããã?
è¨å®ãè¡ãJavaScriptã®ã³ã¼ããå梱ãã¦ãã¾ãã°è¯ãã®ã§ãã
RequireJS2.1ããã¯ãã«ãå¾ã®moduleå
¨ä½ãå²ãwrapã®è¨å®ã§è¤æ°ãã¡ã¤ã«ãæå®ãããã¨ãåºæ¥ãããã«ãªã£ã¦ãã¾ãã
r.js/example.build.js at master · requirejs/r.js · GitHub
wrap.startFileã®2ã¤ç®ä»¥éã«è¨å®ãè¡ãJavaScriptã®ãã¡ã¤ã«ãå
¥ããã°å梱åºæ¥ã¾ãã
ç°å¢å¥ãã«ãçã§ã©ãã©ãæ´»ç¨ãã¦ããã¾ãããã
CommonJS AMDã¨Deferred
Writing Modular JavaScript With AMD, CommonJS & ES Harmonyã®Modules With Deferred Dependenciesã便å©ãªã®ã§æ´»ç¨ãã¦ãã
åæåå¦çãéåæå¦çã§requireãã¦ãã¦ãå³ä½¿ããã¨ã¯éããªãå ´åã«ä½¿ãã
ã¢ã¸ã¥ã¼ã«å´ã§ã¯ã¢ã¸ã¥ã¼ã«ãã®ãã®ã§ã¯ãªãã¦promiseãè¿ãã¦ããã¦ãã¢ã¸ã¥ã¼ã«ã®å®ä½ãå®æãããresolveã§æ¸¡ãã
使ãå´ã¯requireãã¦ããpromiseã®thenã¡ã½ããã®callbackã§ã¢ã¸ã¥ã¼ã«ã®å®ä½ãåãåã£ã¦ä½¿ãã
// ä½ããã®éåæå¦çãçµã¦åæåãããã¢ã¸ã¥ã¼ã« define('someModule',['jquery'],function($){ var dfd = $.Deferred(); setTimeout(function(){ // ã¢ã¸ã¥ã¼ã«ã¨ãã¦å®é使ãããobject var module = {a:1}; dfd.resolve(module); },500); return dfd.promise(); });
// ã¢ã¸ã¥ã¼ã«ã使ãå´ require(['someModule'],function(someModule){ someModule.then(function(module){ console.log(module); // {a:1} }); });
ãããã£ãã¢ã¸ã¥ã¼ã«è¤æ°ã«ä¾åãã¦ããå ´åã¯
require(['someModule1','someModule2','someModule3'],function(someModule1,someModule2,someModule3){ $.when(someModule1,someModule2,someModule3).then(function(module1,module2,module3){ console.log(module1,module2,module3); }); });
ããç°¡åã§ããã?
JavaScriptã§ã³ãã³ããä½ã£ã¦å®è¡ãã
åç½®ã
Firebug1.10ã«helpã³ãã³ããªããã®ãå
¥ã£ã¦ã¾ããã
consoleã§helpã£ã¦å
¥åãã¦å®è¡ããã¨Firebugã§ä½¿ããé¢æ°çã表示ããã¾ãã
help();ãããªããã§ããã
é¢æ°ãããªãã¦ã³ãã³ãã
ã©ããã£ã¦å®ç¾ãã¦ãããã ãããã£ã¦æ°ã«ãªã£ãã®ã§ã¨ã¹ãã¼ãã¦ä¼¼ããããªã®ãä½ã£ã¦ã¿ã¾ããã
Firebugã®ã½ã¼ã¹ã¯èªãã§ãªããã©å¤åãããªããããªãã¨ããã¦ããã¯ãã
ã³ã¼ã
以ä¸ãwithæã®ä¸ã§commandã£ã¦æ¸ãã¦å®è¡ããã¨command is executed.ã£ã¦åºåããã¾ãã
var obj={}; Object.defineProperty(obj,'command',{ get : function() { console.log('command is executed.'); } }); with (obj) { command // command is executed. }
解説
Object.definePropertyã§getã¢ã¯ã»ãµã®ããpropertyãå®ç¾©ããã°ãgetã¢ã¯ã»ãµã®é¢æ°ãé¢æ°ã»ã¡ã½ããå¼ã³åºãã§ã¯ãªãpropertyåç
§ã§å®è¡ã§ãã¾ãã
ããã«withæã§ãã®propertyãæã£ãobjectãæå®ãããã¨ã§ã.ã[]ãªãã§propertyåç
§ã§ããã®ã§ãã³ãã³ãã®ããã«å®è¡ãããã¨ãåºæ¥ãããã«ãªãã¾ãã
Firebugã§ã¯å°ãªãã¨ã以åã¯å
¥åããå
容ãwithæã®ä¸ã§evalãããã¨ã§Firebugå°ç¨ã®é¢æ°çã使ããããã«ãã¦ããã®ã§ãhelpã³ãã³ããããããåãã§ãã
JavaScriptã§ã®built-in/DOM objectã®prototypeæ¡å¼µ
@rosylillyãæ°ã«ãã¦ãã
ã®ã§ã¾ã¨ãããbuilt-in/DOM objectã®prototypeæ¡å¼µã«ããå¼å®³
追å ããããããã£/ã¡ã½ãããfor inã§åæããã
var obj = {a:1}; for (var i in obj) { console.log(i); }
ããããã¨aã ãåºãã¯ããã
Object.prototype.b=function(){};
ããããå¾ã ã¨a,bãåºã¦ãã¾ãã£ã¦å¥´ã§ããã
ãã®ã¾ã¾ä»£å
¥ããªãã§Object.defineProperty/definePropertiesã§enumerable:falseã®ããããã£ã¨ãã¦å®ç¾©ããã°åæãããªããªãã®ã§ç¹ã«åé¡ããã¾ããã
ä»åæããprototypeæ¡å¼µã®å¼å®³ã®å
å¯ä¸ããã ãã¯ECMAScript5æ代ã«ãªã£ã¦è§£æ¶ããã¾ãããå¯ä¸ããã ãã¯ã
built-in/DOM objectã®æ°ããä»æ§ã§è¿½å ãããããããã£/ã¡ã½ããã¨ãããã£ã³ã°
JSã©ãããã¨ãããã©ã®è¨èªã®monkey patchã§ãèµ·ããããåé¡ã
ããè¦ãããä¾
- Prototype.js(1.6.0.3以å)ã§ã®Array#reduceã¨ECMAScript5ã®Array#reduce
- Prototype.js の reduce メソッドを使ってはいけない - Qiita
- å°å ¥ããæç¹ã§ãããã£ã³ã°ãããªãã¾ã ãããã©ãnative Array#reduceç»å ´åããã¢ããªã±ã¼ã·ã§ã³ã³ã¼ãã§ãã£ã¨ä½¿ã£ã¦ã人ã¯å¤§å¤ã ã¨æãã
- Prototype.js(1.7æªæº)ã«ããECMAScript5ã®JSON.stringifyã®ç ´å£
- Ooharabucyou «
- ä½ã ãç¥ãããå£ãã
- ãã¾ã使ããªããJSON.stringifyã§JSONæåååãããobjectã®ã©ããã®é層ã«toJSONã¡ã½ãããæã¤objectãããã¨ãã®æ»ãå¤ã§ãã®é層ãç½®ãæããããããã®toJSONã¡ã½ããã®æ»ãå¤ã¯ããªããã£ã or objectã§ãã£ã¦JSONæååã§ã¯ãªããä¸æ¹Prototype.js(1.7æªæº)ã§ã¯JSONæååãè¿ãArray#toJSONãå®è£ ãã¦ããããJSON.stringifyãã¾ã¨ãã«æ©è½ããªããªã£ã¦ããã
- åé¡ãçºçããã¡ã½ããã¨æ¡å¼µãããç®æãéãããç¥ããªããã°åå ç¹å®ã¯å°é£
JSON.stringify({ b:{ toJSON:function(){return 1;} } }); // '{"b":1}'
ECMAScript6ã§ã便å©ã¡ã½ããã¯è¿½å ãããã®ã§ä»å¾ãä¼¼ããããªç¶æ³ã«ã¯ãªãããã®ã§ãprototypeæ¡å¼µã¨ãã¦ä¾¿å©ã¡ã½ãããå®è£ ããã®ã¯ãå§ããã¾ããã
代æ¡ã¨ãã¦ã¯ãã¯ãã©ããã¼ãªãã¸ã§ã¯ãç³»ã®ã©ã¤ãã©ãªãé¢æ°ç¾¤ãæä¾ããã©ã¤ãã©ãªãè¯ãã¨æãã¾ãã
ã©ããã¼ãªãã¸ã§ã¯ãç³»ã®ã©ã¤ãã©ãª
- DOMã³ã¬ã¯ã·ã§ã³
- Arrayã»Objectç
- String
- Date
æ£ç´Underscore.jsãããããã©ããã¼ãªãã¸ã§ã¯ã/ã¡ã½ãããã§ã¼ã³ã®ã¬ã¤ã¤ã¨ä¾¿å©ã¡ã½ãããçããã¬ã¤ã¤ãåãã¦Underscore.jsçãªã©ã¤ãã©ãªãä½ãåå£ã«ãªã£ãã»ããè¯ãã®ã§ã¯?ãªã©ã¨ã
åãããã«built-in/DOM objectã®prototypeæ¡å¼µããã¦ããä»ã©ã¤ãã©ãªã¨ã®ãããã£ã³ã°
ãããJSã©ãããã¨ãããã©ã®è¨èªã®monkey patchã§ãèµ·ããããåé¡ã
ãããã£ã³ã°ãèµ·ãããªãããã«æ°ãä»ããã¨ããã®ã¯å¯¾çã«ãªãã¾ããã
æåã§scriptã¿ã°ã並ã¹ã¦ããæ代ãªããããããä»ã¯CommonJS AMDçå帰çã«ä¾åé¢ä¿ã辿ãçã£å½ãªã¢ã¸ã¥ã¼ã«ã·ã¹ãã ãããæ代ãªã®ã§ãä¾åé¢ä¿ã°ã©ãã®éä¸ã®ã©ããã¨ã©ããã®ã¢ã¸ã¥ã¼ã«ã§ãããã£ã³ã°ãã¦ããã¨ãç®ãå½ã¦ããã¾ããã
ç©æ¥µçãªmonkey patchãå¿é¿ãããã®ã¯JavaScriptã«éã£ã話ã§ã¯ãªãã¨ãããã¨ãå¿ããã¹ãã§ã¯ããã¾ããã
monkey patchã§ã®ãããã£ã³ã°ã®å¯¾çã¯ä»¥ä¸ã®ãããªãã®ã ã¨æãã¾ã
- è¨èªèªä½ã§å¯¾çãåã
- Ruby2.0ã§å ¥ããããClassBoxç
- JavaScriptã«ã¯ä»ã®æããã¾ãã
- monkey patchã使ç¨ãã¦ããç¹å®ã®ã©ã¤ãã©ãªã»ãã¬ã¼ã ã¯ã¼ã¯ãè¦æ¨©ãæ¡ã£ã¦ãã¾ã
- Rubyçéã§ã¯ã©ã¤ãã©ãªã®monkey patchãActiveSupportsã¨ãããã£ã³ã°ããã¨ãã°æ±ãã«ãªãããã§ãã
- JavaScriptã§ã®monkey patchæ大æã¨ããã°prototype.jsã§ããActiveSupportsã®ãããªæ±ãã¯ããã¦ãã¾ããã
- ã¡ã½ããã»ããããã£åã«prefixãä»ãã¦ãããã£ã³ã°ããªãããã«ãã
- Objective-CçActiveRecordã®GitHub - magicalpanda/MagicalRecord: Super Awesome Easy Fetching for Core Data 1!!!11!!!!1!ãªãããããã§ãã
- prefixãªãã«ãã§ãã¾ã
- Objective-CçActiveRecordã®GitHub - magicalpanda/MagicalRecord: Super Awesome Easy Fetching for Core Data 1!!!11!!!!1!ãªãããããã§ãã
JavaScriptã§ã¯ä¸è¨3対çã®ãã¡äºå®ä¸prefixãä»ãããããããæå¹ã§ã¯ãªãã®ã§ã便å©ã¡ã½ãããç¡éªæ°ã«çããã®ã¯èããã®ã§ãã
Re:prototypeãæ¡å¼µãããã¨ã§å¾ããããã®ãprototypeæ¡å¼µæåã¸ã®ã¹ã¤ãã - latest log
prototypeを拡張することで得られるもの。prototype拡張指向へのスイッチ - latest log
ã¤ãã§ã«ãã®ã¨ã³ããªã«è¨åãã¦ããã¨ããã®ã¨ã³ããªã§ãã£ã¦ãã®ã¯å
¸åçãªè人形ã¡ã½ããã§ãã
prototypeæ¡å¼µã使ããªãåé·ãªã³ã¼ãã¨prototypeæ¡å¼µã使ã£ãèªä½ã©ã¤ãã©ãªã使ã£ãç°¡æ½ãªã³ã¼ããæ¯è¼ãã¦ãã¾ãããåé·ãªã®ã¯prototypeæ¡å¼µã使ã£ã¦ããªãããã§ã¯ããã¾ããã
åé·ãªä½¿ãæ¨ã¦ã³ã¼ãã¨ç°¡æ½ãªAPIã®ã©ã¤ãã©ãªãæ¯è¼ãã¦ããã ãã§ãã
Number#to(prototypeæ¡å¼µãã)ã¨Underscore.js+ECMAScript5æ¨æº(prototypeæ¡å¼µãªã)ãæ¯è¼ãã¦ã¿ã¾ãããã
prototypeæ¡å¼µã«ãã£ã¦è¶ããå£ã¯ã©ããã«ããã¾ãã?
function isX5(n) { // Multiples of 5 filter return n % 5 === 0; } 1..to(100); // -> [1, 2 .. 99, 100] 100..to(1); // -> [100, 99 .. 2, 1] 1..to(100, 2); // -> [1, 3 .. 97, 99] (skip 2) 1..to(100, isX5); // -> [5, 10, .. 95, 100] (filter)
function isX5(n) { // Multiples of 5 filter return n % 5 === 0; } _.range(1,100+1); // [1, 2 .. 99, 100] _.range(1,100+1).reverse(); // [100, 99 .. 2, 1] _.range(1,100+1,2); // [1, 3 .. 97, 99] (skip 2) _.range(1,100+1).filter(isX5); // [5, 10, .. 95, 100] (filter)
built-in/DOM objectã®prototypeæ¡å¼µããã¦ãããããªç®æ
æå¾ã«prototypeæ¡å¼µãæ´»ç¨ã§ããã»ã§ããããªç®æã«ã¤ãã¦ãè¨åãã¦ããã¾ãã
shim/pollyfill
æ¨æºä»æ§ã«å ¥ã£ãAPIãæªå®è£ ã®ç°å¢ã«æä¾ããã®ã¯ä½ãåé¡ããã¾ããããããã£ã³ã°ãã¦åé¡ãçºçãããªãç¸æã®ã»ããæªãã§ããã©ãã©ãããã°ããããããããã©ã¤ãã©ãªãæä¾ãã¦ãã人ã¯ãããããã¾ãã
internalããããã£/ã¡ã½ãã
APIã¨ãã¦å ¬éãããã®ã§ãªãã©ã¤ãã©ãªã®ä¸ã§ã®ã¿ä½¿ãããããã£/ã¡ã½ãããã¾ãä»ã¨ãããã£ã³ã°ããªããããªé·ãååã§è¿½å ããã®ã¯åé¡ããã¾ããããã¡ããenumerableã¯falseã«ãããä¾ãã°GitHub - monjudoh/BeautifulProperties.jsã§Object.prototypeã«å¯¾ãã¦Events.onããã'BeautifulProperties::internalObjectKey'ã¨ããkeyã®ããããã£ã追å ããããããã¯ãããã£ã³ã°ããªãã§ãããã
å°è¦æ¨¡ãªWebWorkerå
ãã®å ´åDOM objectã®prototypeæ¡å¼µã¯ããããããã¾ãããWorkeréã»Worker/windowéã§built-in objectã®prototypeã¯å ±æãããªãã®ã§ãWorkerå ã§ã®æ¡å¼µãä»ã«å½±é¿ãããã¨ã¯ããã¾ãããåã ã®Workerã®scriptã®ãµã¤ãºãprototypeæ¡å¼µã®å½±é¿ãå åã«è¿½ããããè¦æ¨¡ã«æ¢ããã°è¯ãã§ãããã
BPStudy#60 ECMAScript5æ代ã®JavaScriptã©ã¤ãã©ãª
JavaScript: ä¸çã§æã誤解ãããããã°ã©ãã³ã°è¨èª(2001å¹´)
JavaScript: The World's Most Misunderstood Programming Language
http://d.hatena.ne.jp/brazil/20050829/1125321936
2012å¹´æªã 誤解ããã¦ããããã°ã©ãã³ã°è¨èªJavaScript
å«ããã¦ãã"JavaScript"ã¨ã¯ä½ã?
- ECMAScript3
- 13å¹´åã»åä¸ç´(1999å¹´)ã®è¨èªä»æ§
- IE6
- 11å¹´å(2001å¹´)ã®ã©ã³ã¿ã¤ã
å«ããã¦ãã"JavaScript"ã¨åä¸ä»£ã®åè¨èªã®ãã¼ã¸ã§ã³ã¯?
- Python2.1
- Ruby1.6
- Perl5.6
- J2SE1.3
ç¾å¨ã®JavaScriptã¯ECMAScript5
ã©ã®ç°å¢ã§ä½¿ãã?
http://kangax.github.com/es5-compat-table/
ES5 features on iOS/Android's default browser - 愛と勇気と缶ビール
- IE9+(Strict modeãé¤ã)
- Google Chrome13+
- ãå¤æ°åã«ã¼ãå¹ ã¹ãã¼ã¹ä½¿ç¨å¯è½ããé¤ã
- Firefox6+
- Safari5.1.4+
- ãå¤æ°åã«ã¼ãå¹
ã¹ãã¼ã¹ä½¿ç¨å¯è½ããé¤ã
- è¾¼ã¿ãªã6+
- ãå¤æ°åã«ã¼ãå¹
ã¹ãã¼ã¹ä½¿ç¨å¯è½ããé¤ã
- MobileSafari iOS6+
- Android æ¨æºãã©ã¦ã¶ 4.0+
- ãå¤æ°åã«ã¼ãå¹ ã¹ãã¼ã¹ä½¿ç¨å¯è½ããé¤ã
- strict modeãé¤ã
- è¾¼ã¿ãªã4.1+
- Opera12+
- ãå¤æ°åã«ã¼ãå¹ ã¹ãã¼ã¹ä½¿ç¨å¯è½ããé¤ã
shimã©ã¤ãã©ãªã使ãã°å¯¾å¿ç°å¢ã¯ãã£ã¨å¢ãã
GitHub - es-shims/es5-shim: ECMAScript 5 compatibility shims for legacy (and modern) JavaScript engines
- IE9+(Strict modeãé¤ã)
- Google Chrome13+
- ãå¤æ°åã«ã¼ãå¹ ã¹ãã¼ã¹ä½¿ç¨å¯è½ããé¤ã-Firefox6+
- Safari5.1+
- ãå¤æ°åã«ã¼ãå¹
ã¹ãã¼ã¹ä½¿ç¨å¯è½ããé¤ã
- è¾¼ã¿ãªã6+
- ãå¤æ°åã«ã¼ãå¹
ã¹ãã¼ã¹ä½¿ç¨å¯è½ããé¤ã
- MobileSafari iOS5.0+
- ãå¤æ°åã«ã¼ãå¹
ã¹ãã¼ã¹ä½¿ç¨å¯è½ããé¤ã
- è¾¼ã¿ãªã6+-Android æ¨æºãã©ã¦ã¶ 4.0+
- ãå¤æ°åã«ã¼ãå¹ ã¹ãã¼ã¹ä½¿ç¨å¯è½ããé¤ã
- strict modeãé¤ã
- è¾¼ã¿ãªã4.1+
- ãå¤æ°åã«ã¼ãå¹
ã¹ãã¼ã¹ä½¿ç¨å¯è½ããé¤ã
- Opera12+
- ãå¤æ°åã«ã¼ãå¹ ã¹ãã¼ã¹ä½¿ç¨å¯è½ããé¤ã
ä½ãåºæ¥ã?
- strict mode
- Array#mapçã®arrayã®å復ã¡ã½ãã
- ãã®ä»builtin objectã®ä¾¿å©ã¡ã½ãããè¥å¹²
- native JSON
- Function#bind
- thisã¨å¼æ°ã®æç¸
- Object.create
- ã³ã³ã¹ãã©ã¯ã¿ãçµç±ããã«prototypeç¶æ¿ãåºæ¥ã
- Object.prototypeãç¶æ¿ããªãobjectãç´ç²ãªè¾æ¸ãä½ãã
- objectä¿è·é¢æ°ç¾¤
- Object.preventExtensions
- propertyã®è¿½å å®ç¾©ç¦æ¢
- Object.seal
- propertyã®è¿½å å®ç¾©ã»åå®ç¾©ç¦æ¢
- Object.freeze
- propertyã®è¿½å å®ç¾©ã»åå®ç¾©ã»å¤ã®å¤æ´ç¦æ¢
- Object.preventExtensions
- Object.Object.defineProperty
- writable
- enumerable
- configurable
- value
- get
- set
ECMAScript5ã®è±ãããæ´»ããã¦ãããã
ECMAScript5ãã¿ã¼ã²ããã«ããã©ã¤ãã©ãªãBeautifulProperties.js
v0.1.0ãå
ã»ã©ãªãªã¼ã¹ãã¾ãã
GitHub - monjudoh/BeautifulProperties.js at v0.1.0
ã¤ã³ã¹ã¿ã³ã¹ã«ä¾åããåæå¤ãæã¤æ¸ãæãå¯è½propertyã®å®ç¾©
ã¤ã³ã¹ã¿ã³ã¹ã«ä¾åãã¦ãªããã°ããã§æ¸ãããç°¡åã§ãããã¼
function A(){} var proto = A.prototype; Object.defineProperty(proto,'key',{ value : 'default', writable : true });
ã¤ã³ã¹ã¿ã³ã¹ã«ä¾åãã¦ããå ´åã¯prototypeå®ç¾©æã«ãã®ã¤ã³ã¹ã¿ã³ã¹ãåå¨ããªãã®ã§valueã§åæå¤ãå®ç¾©ã§ãã¾ããã
åç´ãªã³ã¼ãã ã¨ãå®éã®å¤ãä¿æããå¥propertyã¨get/setãå®ç¾©ãã¦ãã¾ã ä¿æãã¦ããªãã£ããè¨å®ããã¨ããããªãã§ãããã
function foo(val) { // valã®å 容ã«ãã£ã¦æ»ãå¤ãå¤ããã¨æããã return 'default'; } function A(){} var proto = A.prototype; Object.defineProperty(proto,'key',{ get : function () { var self = this; if (self._key === undefined) { self._key = foo(self); } return self._key; }, set : function (val) { var self = this; self._key = val; } });
ããããç¡é§ã«propertyãå¢ããããããã¾ããã
ãããã¾ãã
prototypeã«å¯¾ãã¦configurable:trueã«ãã¦get/setããã®propertyãå®ç¾©ã
ãã®ä¸ã§ã¤ã³ã¹ã¿ã³ã¹ã®(!=prototypeã®)propertyãä¸æ¸ãå®ç¾©ãã¾ãã
function foo(val) { // valã®å 容ã«ãã£ã¦æ»ãå¤ãå¤ããã¨æããã return 'default'; } function A(){} var proto = A.prototype; Object.defineProperty(proto,'key',{ get : function () { var self = this; var val = foo(self); Object.defineProperty(self,'key',{ value:val, writable:true }); return val; }, set : function (val) { var self = this; Object.defineProperty(self,'key',{ value:val, writable:true }); }, configurable : true });
ã°ã£ã¡ãåãã¾ãã
a= new A; b= new A; a.key; // 'default' a.key= 1; a.key; // 1 b.key; // 'default'
åèhttp://d.hatena.ne.jp/Constellation/20101205/1291564928