�ɼ��ε��������������������ä��Τ����ɡ����֤�ʤ��ΤǾҲ�Ⱦ�Ͽ��
����ץ륳���ɤϡ�Ŭ���ñ¤´ï¿½ï¿½ï¿½ï¿½Æ¤ï¿½ï¿½ï¿½Þ¤ï¿½ï¿½ï¿½
1. Branch when possible - ʬ���Ϥʤ�Ϥ��
����ϼ���򸫤������ᤤ�Ǥ��礦���������֥饦�����б���addListener()
��ͤ��롣��ǽ������ͤ���С��ʲ���OK��
function addListener(el, type, fn) { if ( window.addEventListener ) { el.addEventListener(type, fn, false); } else if ( window.attachEvent ) { var f = function() { fn.call(el, window.event); }; el.attachEvent('on'+type, f); } else { element['on'+type] = fn; } };
�Ǥ⡢�������addListener()
�¹ԤΤ��Ӥˡ��֥饦����Ƚ��ξ��ʬ������ɬ�פ�����ޤ���ɴ��Ȥ���ɴ��֥饦����Ƚ��򤷤Ƥ��ޤ��ΤǤ���
�ʲ��Τ褦�ˤ����С����ʬ���Ϻǽ�ΰ��ǺѤߤޤ���
var addListener = (function() { if ( window.addEventListener ) { return function(el, type, fn) { el.addEventListener(type, fn, false); }; } else if ( window.attachEvent ) { return function(el, type, fn) { var f = function() { fn.call(el, window.event); }; el.attachEvent('on'+type, f); }; } else { return function(el, type, fn) { el['on'+type] = fn; } } })();
2. Make Flags - �ե饰���äƤ�����
�����ǤΥݥ���Ȥϡ�!!expresssion
��expression�η������Ǥ���֡����ͤˤʤ�ޤ����Τ���
function toBool(expression){ return expression ? true : false }
�Ȥ�뤳�Ȥ����ޤ�����!!expresssion
�������ʷ�Ǥ���
3. Make bridges - ���Ϥ������ɤ��Ѱդ��褦
��������ǡ�
addListener(element, 'click', getUserNameById); function getUserNameById(e) { var id = this.id; // id����� }
�Ƚ񤯤ȡ��֥饦������¸�����ɤ�����Ƥ��ޤ��ޤ���this�˲������뤫�ϥ֥饦�����ˤ�äưۤʤ뤫��Ǥ���
�ʲ��Τ褦�˶��Ϥ������ɤ�񤱤С����줬��褷�ޤ���
function getUserNameById(id) { // id����� } function getUserNameByIdBridge (e) { // ����Ƕ��Ϥ� getUserNameById(this.id); } addListener(element, 'click', getUserNameByIdBridge);
4. Try Event Delegation - ���٥�ȰѾ���ȤäƤߤ褦
<ul id="example"> <li>foo</li> <li>bar</li> <li>baz</li> <li>thunk</li> </ul>
�Τ褦��HTML�ˡ�li�򥯥�å������ݤ˲����򤷤����Ȥ��ޤ������ξ�硢li���Ȥ˥��٥�ȥϥ�ɥ顼���ɲä����ꡢ�����Ǥ�ul�˰ʲ��Υϥ�ɥ顼���ɲä���Ф褤�ΤǤ���
var element = document.getElementById('example'); addListener(element, 'click', handleClick); function handleClick(e) { var element = e.target || e.srcElement; // �����element��ul����Υ���å����줿li // element����� }
5. Include methods with your getElementsByWhatever - getElementsByHogeHoge�˥᥽�åɤ��Ϥ���褦�ˤ��Ƥ�����
getElementsByHogeHogge()
�Ȥ�����DOM���椫�鵬������������Ǥ������Ф��Ȥ����ؿ��Ϥ褯�Ȥ����Ȼפ��ޤ��������Ϥ���ʴ�����
function getElementsBySelector(selector) { var elements = []; for ( ... ) { if ( ... ) elements[elements.length] = el; } return elements; }
�����ǡ���������˥�����Хå��ؿ����Ϥ���褦�ˤ��Ƥ����С�����������Ǥ���
function getElementsBySelector(selector, callback) { var elements = []; for ( ... ) { if ( ... ) { elements[elements.length] = el; callback(el); } } return elements; } getElementsBySelector('#example p a', function(el) { // ���� el ����� });
6. Encapsulate your code - �����ɤ򥫥ץ��벽���褦
����ϡ��ؿ��ǥ������פ������Ȥ������ȤǤ��͡�
var a = 'foo'; var b = function() { // �ʤ󤫽��� }; var c = { thunk: function() { }, baz: [false, true, false] };
�Ƚ񤯤��ѿ�a,b,c��Window�������Х�ˤʤäƤ��ޤ��ޤ�����
(function() { var a = 'foo'; var b = function() { // �ʤ󤫽��� }; var c = { thunk: function() { }, baz: [false, true, false] }; }();
�Ȥ���С�Window�������Х���ѿ�����������ޤ���;�̤Ǥ�����Google Analytics��urchin.js�Ϥ���򸫽��ä��ߤ����Ȼפ��ޤ���
7. Reinvent the Wheel - ���ؤ��ȯ�����褦
�����ư��ִ��ä�������Τ��������Ǥ��������������������ޤ���
Digital Web Magazine - Seven JavaScript Techniques You Should Be Using TodayOften, when I sit down to prototype out code or build a widget, I��m almost positive someone has already accomplished what I��m about to attempt. But nevertheless, it��s worth my effort to see if I can do it better. Not every wheel is round. Nor does every round wheel have twenty-inch shiny spinners. Imagination with such a highly flexible language can take you a long way.
[�����ɤ��ñ¤¤ï¿½ï¿½ï¿½ï¿½ê¥¦ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½Ã¥È¤ï¿½ï¿½Ã¤ï¿½ï¿½ê¤·ï¿½Æ¤ï¿½ï¿½ï¿½È¤ï¿½ï¿½ï¿½ï¿½ï¿½Ê¬ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½È¤ï¿½ï¿½Æ¤ï¿½ï¿½ë¤³ï¿½È¤ï¿½ï¿½Ì¤ï¿½Ã¯ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½Ç¤Ë¤ï¿½Ã¤Æ¤ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½È¤ï¿½ï¿½ï¿½ï¿½Î¤ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½Ï°Ï¤Ç¤ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½Ç¤â¡¢ï¿½ï¿½Ã¤È¤ï¿½ï¿½Þ¤ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½Î¤ï¿½ï¿½ï¿½Ê¬ï¿½Ç»î¤¹ï¿½ï¿½ï¿½Í¤Ï¤ï¿½ï¿½ï¿½Þ¤ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½Î¼ï¿½ï¿½Ø¤ï¿½ï¿½ï¿½ï¿½Æ¤ï¿½ï¿½Ý¤ï¿½ï¿½Æ¥Ô¥ï¿½ï¿½Ô¥ï¿½ï¿½È¤ï¿½ï¿½ï¿½ï¿½ï¤±ï¿½Ç¤Ï¤Ê¤ï¿½ï¿½Î¤Ç¤ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½Û¤É½ï¿½ï¿½ï¿½Ê¸ï¿½ï¿½ï¿½Ê¤Î¤Ç¤ï¿½ï¿½ï¿½ï¿½é¡¢ï¿½ï¿½ï¿½ï¿½ï¿½Ï¤Î²Ì¤Æ¤Þ¤Ç¹Ô¤Ã¤Æ¤ï¿½ï¿½Þ¤ï¿½ï¿½ï¿½?]
Perl Monger��¿����JavaScript�ˤ�ϥޤ���ͳ������Ǥ��͡��Ǥ�CPAN�������ߤ����ʤ���function $(id){ return document.getElementById(id) }
���餤�ʤ餤����Ǥ��ȯ�����뤱�ɡ�addLisetner()
���餤�Ǥ�⤦���ԥڤ�����ˤʤ뤷....���Ȥ��äơ�JSAN�Ϥ���ä�CPAN��������褦�ʵ��⤹�뤷....
Dan the Rhino Tamer
�ۤ�Ȥ���ľ���ޤ��������꤬�Ȥ��������ޤ������ε������Ǥ�褯���Ȥ���Ƥ��ޤ���
Dan the Typo Generator