์์ํ์ (Primitive type)์ ๊ทธ ๊ฐ์ ์ง์ ์กฐ์ํฉ๋๋ค.
- string
- number
- boolean
- null
- undefined
var foo = 1,
bar = foo;
bar = 9;
console.log(foo, bar); // 1, 9์ฐธ์กฐํ(Complex)์ ์ฐธ์กฐ๋ฅผ ํตํด ๊ฐ์ ์กฐ์ํฉ๋๋ค.
- object
- array
- function
var foo = [1, 2],
bar = foo;
bar[0] = 9;
console.log(foo[0], bar[0]); // 9, 9-
Object๋ฅผ ๋ง๋ค ๋๋ ๋ฆฌํฐ๋ด ๊ตฌ๋ฌธ์ ์ฌ์ฉํ ๊ฒ
// bad var item = new Object(); // good var item = {};
-
IE8์์ ๋์ํ์ง ์์ผ๋ฏ๋ก ์์ฝ์ด(reserved words)๋ฅผ ํค๋ก ์ฌ์ฉํ์ง ๋ง ๊ฒ
// bad var superman = { default: { clark: 'kent' }, private: true }; // good var superman = { defaults: { clark: 'kent' }, hidden: true };
-
์์ฝ์ด ๋์ ์๊ธฐ ์ฌ์ด ๋์์ด(readable synonyms)๋ฅผ ์ฌ์ฉํ ๊ฒ
// bad var superman = { class: 'alien' }; // bad var superman = { klass: 'alien' }; // good var superman = { type: 'alien' };
-
Array๋ฅผ ๋ง๋ค ๋ ๋ฆฌํฐ๋ด ๊ตฌ๋ฌธ์ ์ฌ์ฉํ ๊ฒ
// bad var items = new Array(); // good var items = [];
-
๊ธธ์ด๋ฅผ ์ ์ ์๋ ๊ฒฝ์ฐ๋ Array#push๋ฅผ ์ฌ์ฉํ ๊ฒ
var someStack = []; // bad someStack[someStack.length] = 'abracadabra'; // good someStack.push('abracadabra');
-
๋ฐฐ์ด์ ๋ณต์ฌ ํ ํ์๊ฐ์๋ ๊ฒฝ์ฐ Array#slice๋ฅผ ์ฌ์ฉํ ๊ฒ
var len = items.length, itemsCopy = [], i; // bad for (i = 0; i < len; i++) { itemsCopy[i] = items[i]; } // good itemsCopy = items.slice();
-
Array์ ๋น์ทํ(Array-Like)ํ Object๋ฅผ Array์ ๋ณํํ๋ ๊ฒฝ์ฐ๋ Array#slice๋ฅผ ์ฌ์ฉํ ๊ฒ
function trigger() { var args = Array.prototype.slice.call(arguments); ... }
-
๋ฌธ์์ด์
์์ ๋ฐ์ดํ('')๋ฅผ ์ฌ์ฉํ ๊ฒ// bad var name = "Bob Parr"; // good var name = 'Bob Parr'; // bad var fullName = "Bob " + this.lastName; // good var fullName = 'Bob ' + this.lastName;
-
80 ๋ฌธ์ ์ด์์ ๋ฌธ์์ด์ ๋ฌธ์์ด ์ฐ๊ฒฐ์ ์ฌ์ฉํ์ฌ ์ฌ๋ฌ ์ค์ ๊ฑธ์ณ ๊ธฐ์ ํ ๊ฒ
- ๋ฌธ์์ด ์ฐ๊ฒฐ์ ๋จ์ฉํ๋ฉด ์ฑ๋ฅ์ ์ํฅ์ ์ค ์ ์์s
// bad var errorMessage = 'This is a super long error that was thrown because of Batman. When you stop to think about how Batman had anything to do with this, you would get nowhere fast.'; // bad var errorMessage = 'This is a super long error that \ was thrown because of Batman. \ When you stop to think about \ how Batman had anything to do \ with this, you would get nowhere \ fast.'; // good var errorMessage = 'This is a super long error that ' + 'was thrown because of Batman.' + 'When you stop to think about ' + 'how Batman had anything to do ' + 'with this, you would get nowhere ' + 'fast.';
- ๋ฌธ์์ด ์ฐ๊ฒฐ์ ๋จ์ฉํ๋ฉด ์ฑ๋ฅ์ ์ํฅ์ ์ค ์ ์์s
-
ํ๋ก๊ทธ๋จ์์ ๋ฌธ์์ด์ ์์ฑ ํ ํ์๊ฐ ์๋ ๊ฒฝ์ฐ (ํนํ IE๋) ๋ฌธ์์ด ์ฐ๊ฒฐ ๋์ Array#join์ ์ฌ์ฉํ ๊ฒ
var items, messages, length, i; messages = [{ state: 'success', message: 'This one worked.' },{ state: 'success', message: 'This one worked as well.' },{ state: 'error', message: 'This one did not work.' }]; length = messages.length; // bad function inbox(messages) { items = '<ul>'; for (i = 0; i < length; i++) { items += '<li>' + messages[i].message + '</li>'; } return items + '</ul>'; } // good function inbox(messages) { items = []; for (i = 0; i < length; i++) { items[i] = messages[i].message; } return '<ul><li>' + items.join('</li><li>') + '</li></ul>'; }
-
ํจ์์(Function expressions)
// ์ต๋ช ํจ์์(anonymous function expression) var anonymous = function() { return true; }; // ๋ช ๋ช ๋ ํจ์์(named function expression) var named = function named() { return true; }; // ์ฆ์์คํ ํจ์์(immediately-invoked function expression (IIFE)) (function() { console.log('Welcome to the Internet. Please follow me.'); })();
if ๋ฐ while ๋ฑ๋ธ๋ก ๋ด์์ ๋ณ์์ ํจ์๋ฅผ ํ ๋นํ๋ ๋์ ํจ์๋ฅผ ์ ์ธํ์ง ๋ง ๊ฒ- ๋ธ๋ผ์ฐ์ ๋ ํ์ฉํ์ง๋ง ๋ชจ๋ ๋ค๋ฅธ ๋ฐฉ์์ผ๋ก ํด์
- ECMA-262์์๋ block์ statements์ ๋ชฉ๋ก์ ์ ์๋์ด ์์ผ๋ ํจ์ ์ ์ธ์ statements๊ฐ ์์
// bad if (currentUser) { function test() { console.log('Nope.'); } } // good var test; if (currentUser) { test = function test() { console.log('Yup.'); }; }
-
๋งค๊ฐ ๋ณ์(parameter)์ arguments๋ฅผ ์ ๋ ์ง์ ํ์ง ๋ง ๊ฒ
- ์ด๊ฒ์ ํจ์ ๋ฒ์๋ก ์ ๋ฌ ๋ arguments ๊ฐ์ฒด์ ์ฐธ์กฐ๋ฅผ ๋ฎ์ด์
// bad function nope(name, options, arguments) { // ...stuff... } // good function yup(name, options, args) { // ...stuff... }
- ์ด๊ฒ์ ํจ์ ๋ฒ์๋ก ์ ๋ฌ ๋ arguments ๊ฐ์ฒด์ ์ฐธ์กฐ๋ฅผ ๋ฎ์ด์
-
์์ฑ์ ์ก์ธ์คํ๋ ค๋ฉด
๋ํธ(.)๋ฅผ ์ฌ์ฉํ ๊ฒvar luke = { jedi: true, age: 28 }; // bad var isJedi = luke['jedi']; // good var isJedi = luke.jedi;
-
๋ณ์๋ฅผ ์ฌ์ฉํ์ฌ ์์ฑ์ ์ ๊ทผํ๋ ค๋ฉด
๋๊ดํธ([])๋ฅผ ์ฌ์ฉํ ๊ฒvar luke = { jedi: true, age: 28 }; function getProp(prop) { return luke[prop]; } var isJedi = getProp('jedi');
-
๋ณ์๋ฅผ ์ ์ธ ํ ๋๋ ํญ์
var๋ฅผ ์ฌ์ฉํ ๊ฒ.- ๊ทธ๋ ์ง ์์ผ๋ฉด ์ ์ญ ๋ณ์๋ก ์ ์ธ๋จ
- ์ ์ญ ๋ค์ ์คํ์ด์ค๋ฅผ ์ค์ผ์ํค์ง ์๋๋ก Captain Planet๋ ๊ฒฝ๊ณ ํจ
// bad superPower = new SuperPower(); // good var superPower = new SuperPower();
-
์ฌ๋ฌ ๋ณ์๋ฅผ ์ ์ธํ๋ ค๋ฉด ํ๋์ var๋ฅผ ์ฌ์ฉํ์ฌ ๋ณ์๋ง๋ค ์ค๋ฐ๊ฟํ์ฌ ์ ์ธํ ๊ฒ
// bad var items = getItems(); var goSportsTeam = true; var dragonball = 'z'; // good var items = getItems(), goSportsTeam = true, dragonball = 'z';
-
์ ์๋์ง ์์ ๋ณ์๋ฅผ ๋ง์ง๋ง์ผ๋ก ์ ์ธํ ๊ฒ
- ๋์ค์ ์ด๋ฏธ ํ ๋น๋ ๋ณ์ ์ค ํ๋๋ฅผ ์ง์ ํด์ผํ๋ ๊ฒฝ์ฐ์ ์ ์ฉ
// bad var i, len, dragonball, items = getItems(), goSportsTeam = true; // bad var i, items = getItems(), dragonball, goSportsTeam = true, len; // good var items = getItems(), goSportsTeam = true, dragonball, length, i;
- ๋์ค์ ์ด๋ฏธ ํ ๋น๋ ๋ณ์ ์ค ํ๋๋ฅผ ์ง์ ํด์ผํ๋ ๊ฒฝ์ฐ์ ์ ์ฉ
-
๋ณ์์ ํ ๋น์ ์ค์ฝํ์ ์์ ๋ถ๋ถ์์ ํ๋ฉฐ ์ด๊ฒ์ ๋ณ์ ์ ์ธ๊ณผ Hoisting ๊ด๋ จ ๋ฌธ์ ๋ฅผ ํด๊ฒฐํจ
// bad function() { test(); console.log('doing stuff..'); //..other stuff.. var name = getName(); if (name === 'test') { return false; } return name; } // good function() { var name = getName(); test(); console.log('doing stuff..'); //..other stuff.. if (name === 'test') { return false; } return name; } // bad function() { var name = getName(); if (!arguments.length) { return false; } return true; } // good function() { if (!arguments.length) { return false; } var name = getName(); return true; }
-
ํด๋น ์ค์ฝํ์ ์์ ๋ถ๋ถ์ Hoist๋ ๋ณ์์ ์ธ์ ํ ๋น๋์ง ์์
// (notDefined๊ฐ ์ ์ญ ๋ณ์์ ์กด์ฌํ์ง ์๋๋ค๊ณ ๊ฐ์ ํ์ ๊ฒฝ์ฐ) // ์ด๊ฒ์ ๋์ํ์ง ์์ต๋๋ค. function example() { console.log(notDefined); // => throws a ReferenceError } // ๊ทธ ๋ณ์๋ฅผ ์ฐธ์กฐํ๋ ์ฝ๋ ํ์ ๊ทธ ๋ณ์๋ฅผ ์ ์ธ ํ ๊ฒฝ์ฐ // ๋ณ์๊ฐ Hoist๋ ์ํ์์ ์๋ํฉ๋๋ค. // Note : `true`๋ผ๋ ๊ฐ ์์ฒด๋ Hoist๋์ง ์์ต๋๋ค. function example() { console.log(declaredButNotAssigned); // => undefined var declaredButNotAssigned = true; } // ์ธํฐ ํ๋ฆฐํฐ๋ ๋ณ์ ์ ์ธ์ ์ค์ฝํ์ ์์ ๋ถ๋ถ์ Hoistํฉ๋๋ค. // ์์ ์๋ ๋ค์๊ณผ ๊ฐ์ด ๋ค์ ์์ฑํ ์ ์์ต๋๋ค. function example() { var declaredButNotAssigned; console.log(declaredButNotAssigned); // => undefined declaredButNotAssigned = true; }
-
์ต๋ช ํจ์์ ๊ฒฝ์ฐ ํจ์๊ฐ ํ ๋น๋๊ธฐ ์ ์ ๋ณ์๊ฐ Hoist๋ ์ ์์
function example() { console.log(anonymous); // => undefined anonymous(); // => TypeError anonymous is not a function var anonymous = function() { console.log('anonymous function expression'); }; }
-
๋ช ๋ช ๋ ํจ์์ ๊ฒฝ์ฐ๋ ๋ง์ฐฌ๊ฐ์ง๋ก ๋ณ์๊ฐ Hoist๋ ์ ์์ผ๋ ํจ์ ์ด๋ฆ๊ณผ ํจ์ ๋ณธ์ฒด๋ Hoist๋์ง ์์
function example() { console.log(named); // => undefined named(); // => TypeError named is not a function superPower(); // => ReferenceError superPower is not defined var named = function superPower() { console.log('Flying'); }; } // ํจ์์ด๋ฆ๊ณผ ๋ณ์์ด๋ฆ์ด ๊ฐ์ ๊ฒฝ์ฐ์๋ ๊ฐ์ ์ผ์ด ์ผ์ด๋ฉ๋๋ค. function example() { console.log(named); // => undefined named(); // => TypeError named is not a function var named = function named() { console.log('named'); } }
-
ํจ์ ์ ์ธ์ ํจ์์ด๋ฆ๊ณผ ํจ์๋ณธ๋ฌธ์ด Hoist ๋จ
function example() { superPower(); // => Flying function superPower() { console.log('Flying'); } }
-
== ๋ != ๋ณด๋ค๋ === ์ !== ๋ฅผ ์ฌ์ฉํ ๊ฒ
-
์กฐ๊ฑด์์ ToBoolean ๋ฉ์๋์ ์ํด ์๋์ ๊ฐ์ด ์๋ฐํ๊ฒ ๋น๊ต
- Objects ๋ true ๋ก ํ๊ฐ
- undefined ๋ false ๋ก ํ๊ฐ
- null ๋ false ๋ก ํ๊ฐ
- Booleans ๋ boolean ํ์ ๊ฐ์ผ๋ก ํ๊ฐ
- Numbers ๋ true ๋ก ํ๊ฐ๋๋ +0, -0, or NaN ์ ๊ฒฝ์ฐ๋ false
- Strings ๋ true ๋ก ํ๊ฐ๋๋ ๋น๋ฌธ์ '' ์ ๊ฒฝ์ฐ๋ false
if ([0]) { // true // Array๋ Object ์ด๋ฏ๋ก true ๋ก ํ๊ฐ๋ฉ๋๋ค. }
-
์งง์ ํ์์ ์ฌ์ฉํ ๊ฒ
// bad if (name !== '') { // ...stuff... } // good if (name) { // ...stuff... } // bad if (collection.length > 0) { // ...stuff... } // good if (collection.length) { // ...stuff... }
- ๋ณต์ํ ๋ธ๋ก์
์ค๊ดํธ({})๋ฅผ ์ฌ์ฉํ ๊ฒ// bad if (test) return false; // good if (test) return false; // good if (test) { return false; } // bad function() { return false; }Comments // good function() { return false; }
-
๋ณต์ํ์ ์ฝ๋ฉํธ๋
/** ... */๋ฅผ ์ฌ์ฉํ ๊ฒ- ๊ทธ ์์๋ ์ค๋ช
๊ณผ ๋ชจ๋ ๋งค๊ฐ ๋ณ์์ ๋ฐํ ๊ฐ์ ๋ํ ํ์๊ณผ ๊ฐ์ ์ค๋ช
// bad // make() returns a new element // based on the passed in tag name // // @param <String> tag // @return <Element> element function make(tag) { // ...stuff... return element; } // good /** * make() returns a new element * based on the passed in tag name * * @param <String> tag * @return <Element> element */ function make(tag) { // ...stuff... return element; }
- ๊ทธ ์์๋ ์ค๋ช
๊ณผ ๋ชจ๋ ๋งค๊ฐ ๋ณ์์ ๋ฐํ ๊ฐ์ ๋ํ ํ์๊ณผ ๊ฐ์ ์ค๋ช
-
ํ ์ค ์ฃผ์์๋
// ...๋ฅผ ์ฌ์ฉํ ๊ฒ- ์ฝ๋ฉํธ๋ฅผ ์ถ๊ฐํ๊ณ ์ถ์ ์ฝ๋์ ์๋จ์ ์์ฑํ๊ณ , ์ฃผ์ ์์ ๋น ์ค์ ๋ฃ์ ๊ฒ
// bad var active = true; // is current tab // good // is current tab var active = true; // bad function getType() { console.log('fetching type...'); // set the default type to 'no type' var type = this._type || 'no type'; return type; } // good function getType() { console.log('fetching type...'); // set the default type to 'no type' var type = this._type || 'no type'; return type; }
- ์ฝ๋ฉํธ๋ฅผ ์ถ๊ฐํ๊ณ ์ถ์ ์ฝ๋์ ์๋จ์ ์์ฑํ๊ณ , ์ฃผ์ ์์ ๋น ์ค์ ๋ฃ์ ๊ฒ
-
๋ฌธ์ ๋ฅผ ์ง์ ํ๊ณ ์ฌ๊ณ ๋ฅผ ์ด๊ตฌํ๊ฑฐ๋ ๋ฌธ์ ์ ๋ํ ํด๊ฒฐ์ฑ ์ ์ ์ํ๋ ๋ฑ ์๊ฒฌ์ ์์ FIXME ๋ TODO๋ฅผ ๋ถ์ผ ๊ฒ
- ๋ค๋ฅธ ๊ฐ๋ฐ์์ ๋น ๋ฅธ ์ดํด๋ฅผ ๋์ฐ๋ฉฐ ์ด๋ฌํ ์ด๋ค ์ก์ ์ ๋๋ฐํ๋ค๋ ์๋ฏธ์์ ์ผ๋ฐ ์ฝ๋ฉํธ์๋ ๋ค๋ฆ
- ์ก์
์
FIXME - ํด๊ฒฐ์ฑ ์ด ํ์๋๋TODO - ๊ตฌํ์ด ํ์
-
๋ฌธ์ ์ ๋ํ ์ฝ๋ฉํธ๋ก
// FIXME :๋ฅผ ์ฌ์ฉํ ๊ฒfunction Calculator() { // FIXME: ์ ์ญ ๋ณ์๋ฅผ ์ฌ์ฉํด์๋ ์๋ฉ๋๋ค. total = 0; return this; }
-
๋ฌธ์ ํด๊ฒฐ์ฑ ์ ๋ํ ์ฝ๋ฉํธ๋ก
// TODO :๋ฅผ ์ฌ์ฉํ ๊ฒfunction Calculator() { // TODO: total์ ์ต์ ๋งค๊ฐ ๋ณ์๋ก ์ค์ ๋์ด์ผ ํจ. this.total = 0; return this; }
-
ํญ์๋ ๊ณต๋ฐฑ 2๊ฐ๋ฅผ ์ค์ ํ ๊ฒ
// bad function() { โโโโvar name; } // bad function() { โvar name; } // good function() { โโvar name; } -
์ค๊ดํธ({})์ ์์ ๊ณต๋ฐฑ์ ํ๋ ๋ฃ์ ๊ฒ
// bad function test(){ console.log('test'); } // good function test() { console.log('test'); } // bad dog.set('attr',{ age: '1 year', breed: 'Bernese Mountain Dog' }); // good dog.set('attr', { age: '1 year', breed: 'Bernese Mountain Dog' });
-
ํ์ผ์ ๋ง์ง๋ง์๋ ๋น ์ค์ ํ๋ ๋ฃ์ด์ฃผ์ญ์์ค.
// bad (function(global) { // ...stuff... })(this);
// good (function(global) { // ...stuff... })(this);
-
๋ฉ์๋ ์ฒด์ธ์ด ๊ธธ์ด์ง๋ ๊ฒฝ์ฐ ์ ์ ํ ๋ค์ฌ์ฐ๊ธฐ(indentation)๋ฅผ ํ ๊ฒ
// bad $('#items').find('.selected').highlight().end().find('.open').updateCount(); // good $('#items') .find('.selected') .highlight() .end() .find('.open') .updateCount(); // bad var leds = stage.selectAll('.led').data(data).enter().append('svg:svg').class('led', true) .attr('width', (radius + margin) * 2).append('svg:g') .attr('transform', 'translate(' + (radius + margin) + ',' + (radius + margin) + ')') .call(tron.led); // good var leds = stage.selectAll('.led') .data(data) .enter().append('svg:svg') .class('led', true) .attr('width', (radius + margin) * 2) .append('svg:g') .attr('transform', 'translate(' + (radius + margin) + ',' + (radius + margin) + ')') .call(tron.led);
-
์ ๋์ comma๋ ํ์ง ๋ง ๊ฒ
// bad var once , upon , aTime; // good var once, upon, aTime; // bad var hero = { firstName: 'Bob' , lastName: 'Parr' , heroName: 'Mr. Incredible' , superPower: 'strength' }; // good var hero = { firstName: 'Bob', lastName: 'Parr', heroName: 'Mr. Incredible', superPower: 'strength' };
-
๋ง๋ฏธ์ ๋ถํ์ํ ์ผํ๋ ํ์ง ๋ง ๊ฒ. ์ด๊ฒ์ IE6/7๊ณผ quirksmode์ IE9์์ ๋ฌธ์ ๋ฅผ ์ผ์ผํฌ ์ ์์ผ๋ฉฐ ES3์ ์ผ๋ถ ๊ตฌํ์์ ๋ถํ์ํ ์ผํ๊ฐ ์๋ ๊ฒฝ์ฐ, ๋ฐฐ์ด ๊ธธ์ด๋ฅผ ์ถ๊ฐํจ
// bad var hero = { firstName: 'Kevin', lastName: 'Flynn', }; var heroes = [ 'Batman', 'Superman', ]; // good var hero = { firstName: 'Kevin', lastName: 'Flynn' }; var heroes = [ 'Batman', 'Superman' ];
- ์๋์ ๊ฐ์ด ์ฌ์ฉํ ๊ฒ
// bad (function() { var name = 'Skywalker' return name })() // good (function() { var name = 'Skywalker'; return name; })(); // good ;(function() { var name = 'Skywalker'; return name; })();
-
๋ฌธ์ ์์ ๋ถ๋ถ์์ ํ์ ๊ฐ์ ํ ๊ฒ
-
Strings:
// => this.reviewScore = 9; // bad var totalScore = this.reviewScore + ''; // good var totalScore = '' + this.reviewScore; // bad var totalScore = '' + this.reviewScore + ' total score'; // good var totalScore = this.reviewScore + ' total score';
-
์ซ์๋ parseInt๋ฅผ ์ฌ์ฉํ ๊ฒ. ํญ์ ํ๋ณํ์ ์ํ
๊ธฐ์(radix)๋ฅผ ์ธ์๋ก ์ ๋ฌํ ๊ฒvar inputValue = '4'; // bad var val = new Number(inputValue); // bad var val = +inputValue; // bad var val = inputValue >> 0; // bad var val = parseInt(inputValue); // good var val = Number(inputValue); // good var val = parseInt(inputValue, 10);
-
์ด๋ค ์ด์ ์ ์ํด parseInt๊ฐ ๋ณ๋ชฉ์ด ๋๊ณ , ์ฑ๋ฅ์ ์ธ ์ด์ ๋ก Bitshift๋ฅผ ์ฌ์ฉํ ํ์๊ฐ ์์ ๊ฒฝ์ฐ, ํ๋ ค๊ณ ํ๋๊ฒ์ ๋ํด why(์)์ what(๋ฌด์)์ ์ค๋ช ์ ์ฝ๋ฉํธ๋ก ๋จ๊ธธ ๊ฒ
// good /** * parseInt๊ฐ ๋ณ๋ชฉ์ ์ผ์ผํค๋ฏ๋ก * Bitshift๋ก ๋ฌธ์์ด์ ์์น๋ก ๊ฐ์ ์ ์ผ๋ก ๋ณํํ๋ ๋ฐฉ๋ฒ์ผ๋ก * ์ฑ๋ฅ์ ๊ฐ์ ์ํต๋๋ค. */ var val = inputValue >> 0;
-
Booleans:
var age = 0; // bad var hasAge = new Boolean(age); // good var hasAge = Boolean(age); // good var hasAge = !!age;
-
ํ๋ฌธ์ ์ด๋ฆ์ ํผํ๊ณ ์ด๋ฆ์์ ์๋๋ฅผ ์ฝ์ ์ ์๋๋ก ํ ๊ฒ
// bad function q() { // ...stuff... } // good function query() { // ..stuff.. }
-
Object, ํจ์, ๊ทธ๋ฆฌ๊ณ ์ธ์คํด์ค๋ก๋ camelCase๋ฅผ ์ฌ์ฉํ ๊ฒ
// bad var OBJEcttsssss = {}; var this_is_my_object = {}; var this-is-my-object = {}; function c() {}; var u = new user({ name: 'Bob Parr' }); // good var thisIsMyObject = {}; function thisIsMyFunction() {}; var user = new User({ name: 'Bob Parr' });
-
Class์ ์์ฑ์์๋ PascalCase๋ฅผ ์ฌ์ฉํ ๊ฒ
// bad function user(options) { this.name = options.name; } var bad = new user({ name: 'nope' }); // good function User(options) { this.name = options.name; } var good = new User({ name: 'yup' });
-
private ์์ฑ ์ด๋ฆ์ ๋ฐ์ค _ ์ ์ฌ์ฉํ ๊ฒ
// bad this.__firstName__ = 'Panda'; this.firstName_ = 'Panda'; // good this._firstName = 'Panda';
-
this์ ์ฐธ์กฐ๋ฅผ ์ ์ฅํ ๋ _this ๋ฅผ ์ฌ์ฉํ ๊ฒ
// bad function() { var self = this; return function() { console.log(self); }; } // bad function() { var that = this; return function() { console.log(that); }; } // good function() { var _this = this; return function() { console.log(_this); }; }
-
ํจ์์ ์ด๋ฆ์ ๋ถ์ฌ stack traces๋ฅผ ์ถ์ ํ๊ธฐ ์ฝ๊ฒํ ๊ฒ
// bad var log = function(msg) { console.log(msg); }; // good var log = function log(msg) { console.log(msg); };
-
์ Object์์ ํ๋กํ ํ์ ์ ์ฌ์ ์ํ๋ ๊ฒ์ด ์๋๋ผ ํ๋กํ ํ์ ๊ฐ์ฒด์ ๋ฉ์๋๋ฅผ ์ถ๊ฐํ ๊ฒ
- ํ๋กํ ํ์ ์ ์ฌ์ ์ํ๋ฉด ์์์ด ๋ถ๊ฐ๋ฅํจ
- ํ๋กํ ํ์
์ ๋ฆฌ์
ํ๋๊ฒ์ผ๋ก ๋ฒ ์ด์ค ํด๋์ค๋ฅผ ์ฌ์ ์ ํ ์ ์์
function Jedi() { console.log('new jedi'); } // bad Jedi.prototype = { fight: function fight() { console.log('fighting'); }, block: function block() { console.log('blocking'); } }; // good Jedi.prototype.fight = function fight() { console.log('fighting'); }; Jedi.prototype.block = function block() { console.log('blocking'); };
-
๋ฉ์๋์ ๋ฐํ ๊ฐ์ผ๋ก this๋ฅผ ๋ฐํํจ์ผ๋ก์จ ๋ฉ์๋ ์ฒด์ธ์ ํ ์ ์์ต๋๋ค.
// bad Jedi.prototype.jump = function() { this.jumping = true; return true; }; Jedi.prototype.setHeight = function(height) { this.height = height; }; var luke = new Jedi(); luke.jump(); // => true luke.setHeight(20) // => undefined // good Jedi.prototype.jump = function() { this.jumping = true; return this; }; Jedi.prototype.setHeight = function(height) { this.height = height; return this; }; var luke = new Jedi(); luke.jump() .setHeight(20);
-
๋ ์์ ์ธ toString()์ ๋ง๋ค ์๋ ์์ง๋ง ์ฌ๋ฐ๋ฅด๊ฒ ์๋ํ๋์ง, ๋ถ์์ฉ์ด ์๋ ๊ฒ๋ง์ ํ์ธํด ์ฃผ์ญ์์ค.
function Jedi(options) { options || (options = {}); this.name = options.name || 'no name'; } Jedi.prototype.getName = function getName() { return this.name; }; Jedi.prototype.toString = function toString() { return 'Jedi - ' + this.getName(); };
-
DOM ์ด๋ฒคํธ๋ Backbone events์ ๊ฐ์ ๊ณ ์ ์ ์ด๋ฒคํธ ํ์ฌ์ฒด(payloads)์ ๊ฐ์ ์ ๋ฌํ๋ ๊ฒฝ์ฐ ์์ ๊ฐ(raw value) ๋์ ํด์ ์ธ์(hash)๋ฅผ ์ ๋ฌ
- ์ด๋ ๊ฒํ๋ ๊ฒ์ผ๋ก ๋์ค์ ๊ฐ๋ฐ์๊ฐ ์ด๋ฒคํธ์ ๊ด๋ จ๋ ๋ชจ๋ ํธ๋ค๋ฌ๋ฅผ ์ฐพ์ ์ ๋ฐ์ดํธ ํ์ง ์๊ณ ์ด๋ฒคํธ ํ์ฌ์ฒด(payloads)์ ๊ฐ์ ์ถ๊ฐ ํ ์ ์์
-
์ด๊ฒ๋ณด๋จ
// bad $(this).trigger('listingUpdated', listing.id); ... $(this).on('listingUpdated', function(e, listingId) { // do something with listingId });
-
์ด๊ฒ ์ข์
// good $(this).trigger('listingUpdated', { listingId : listing.id }); ... $(this).on('listingUpdated', function(e, data) { // do something with data.listingId });
- ๋ชจ๋์ ์์์ ! ๋ก ์์ํ ๊ฒ
- ์ด ํ์๋ ๋ฌธ๋ง์ ์ธ๋ฏธ์ฝ๋ก ์ ๋ฃ๋ ๊ฒ์ ์์ ๋ชจ๋์ ์ฐ๊ฒฐํ ๋ ๋ฐํ์ ์ค๋ฅ๊ฐ ๋ฐ์ํ๋ ๊ฒ์ ๋ฐฉ์งํจ
- ํ์ผ ์ด๋ฆ์ camelCase๋ฅผ ์ฌ์ฉํ์ฌ ๊ฐ์ ์ด๋ฆ์ ํด๋์ ์ ์ฅํ๊ณ , ๋จ๋ ์ผ๋ก ๊ณต๊ฐํ ๊ฒฝ์ฐ ์ด๋ฆ์ ์ผ์น์ํฌ ๊ฒ
- noConflict() ๋ผ๋ ๋ช ์นญ์ผ๋ก (์ด๋ฆ์ด ๊ฒน์ณ ๋ฎ์ด ์จ์ง๊ธฐ ์ ์) ๋ชจ๋์ ๋ฐํํ๋ ๋ฉ์๋๋ฅผ ์ถ๊ฐํ ๊ฒ
- ํญ์ ๋ชจ๋์ ์์ ๋ถ๋ถ์์
'use strict';๋ฅผ ์ ์ธํ ๊ฒ// fancyInput/fancyInput.js !function(global) { 'use strict'; var previousFancyInput = global.FancyInput; function FancyInput(options) { this.options = options || {}; } FancyInput.noConflict = function noConflict() { global.FancyInput = previousFancyInput; return FancyInput; }; global.FancyInput = FancyInput; }(this);
-
jQuery Object์ ๋ณ์ ์์๋ $์ ๋ถ์ฌํ ๊ฒ
// bad var sidebar = $('.sidebar'); // good var $sidebar = $('.sidebar');
-
jQuery ์ฟผ๋ฆฌ๊ฒฐ๊ณผ๋ฅผ ์บ์ํ ๊ฒ
// bad function setSidebar() { $('.sidebar').hide(); // ...stuff... $('.sidebar').css({ 'background-color': 'pink' }); } // good function setSidebar() { var $sidebar = $('.sidebar'); $sidebar.hide(); // ...stuff... $sidebar.css({ 'background-color': 'pink' }); }
-
DOM ๊ฒ์์ Cascading
$('.sidebar ul') ์ด๋ parent > child $ ('.sidebar > ul') ๋ฅผ ์ฌ์ฉํ ๊ฒ -
jQuery Object ๊ฒ์์ ์ค์ฝํ๊ฐ ๋ถ์ find๋ฅผ ์ฌ์ฉํ ๊ฒ
// bad $('ul', '.sidebar').hide(); // bad $('.sidebar').find('ul').hide(); // good $('.sidebar ul').hide(); // good $('.sidebar > ul').hide(); // good $sidebar.find('ul');