검색결과 리스트
글
자바스크립트를 하면서 실제로 사용했던 것들과 사용하면 유용할 것들이 보여 여기에 정리해둔다.
Javascript is so much fun, except when it’s not.
There’s always the fear of runtime errors that keeps us thinking all the time while writing code. It makes us better coders - we have no other option than to visualize every line of code as if it’s running as we write it.
That’s why it’s so important to have tidy code. Small code. Pretty code. Code you just fall in love with. Otherwise, Javascript will scare you away.
I gathered some fun snippets I enjoy using instead of boring code that takes too much space. Some makes the code shorter, cleaner and more readable. Other are just plain hacks for debugging.
I learned all of this from open source code (until node.js all javascript code was open source, wasn’t it?), but I’ll write them here is if I invented them.
Hipster Hack #1 - Method calling
I really hate if/else blocks and this is quite a useful trick to call the right function based on a boolean value.
// Boring
// Hipster-fun
Hipster Hack #2 - String joins
It’s a known fact that strings like other strings. Sooner or later you’d like to concatenate two or more of them. I don’t really like +ing them together, so join() comes to the rescue.
['first', 'name'].join(' '); // = 'first name';
['milk', 'coffee', 'sugar'].join(', '); // = 'milk, coffee, sugar'
Hipster Hack #3 - Default Operator ||
Javascript is all about not knowing what an object holds. Sometime you get it as a function argument, other times you might read it from the network or a configuration file. Either way, you can use the || operator to use the second argument if the first is falsy.
// default to 'No name' when myName is empty (or null, or undefined)
Hipster Hack #4 - Guard Operator &&
Similar to the Default Operator, this one is super useful. It eliminates almost all IF calls and makes for a nicer code.
// Boring
// Awesome
// Also cool for guarding your code
추가) 2017-01-25
Hipster Hack #5 - XXX Operator
This one is totally copyrighted and also SFW. Whenever I write some code, but then have to consult the web, or a different part of the code, I add an xxx line to the code. This makes the code break so I can get back to the specific place and fix it later. Much easier to search for it (xxx usually never appears) and you don’t have to think about a TODO comment.
var z = 15;
Hipster Hack #6 - Timing
Ever wonder what’s faster: Looping with an i++ or looping with an i— ? Yeah, me neither. For those who does, you can use the console’s timing methods to test for slow loops or any other event-loop blocking code.
var a = [1,2,3,4,5,6,7,8,9,10];
console.time('testing_forward');
console.time('testing_backwards');
Hipster Hack #7 - Debugging
I learned this one from a Java developer. I have absolutely no idea how he knew about it and I didn’t, but I’ve been using it ever since. Just drop a debugger statement and the debugger will stop on that line.
var x = 1;
var x = Math.random(2);
Hipster Hack #8 - Old School Debugging
I’ve always been a “printf debugger” more than a line-by-line-in-a-debugger kind of developer. If you’re like me, you’ll want to “export” some private vars into the global scope in order to examine them from time to time. Don’t forget to remove these before committing/pushing-to-production.
var deeplyNestedFunction = function() {
// Globalize it for debugging:
// Now from the console (Chrome dev tools, firefox tools, etc)
Hipster Hack #9 - Ultra Light Templates (Great!!!)
Are you still concatenating strings using the + operator? Here’s a better way to combine a sentence with your data. It’s called templating and here’s a mini framework in 2.5 lines of code.
var firstName = 'Tal';
// Ugly
// Super
추가) 2017-01-25
출처 : http://berzniz.com/post/68001735765/javascript-hacks-for-hipsters
'-- JavaScript' 카테고리의 다른 글
다른 도메인에서 함수 호출 (0) | 2014.11.24 |
---|---|
유튜브 동영상 종료 시 처리하기 (0) | 2014.08.21 |
[CrossBrowser] IE7, 8, 9에서 주의사항 (0) | 2014.07.01 |
javascript replace (0) | 2014.05.21 |
javascript로 number_format 구현 (0) | 2013.09.26 |
RECENT COMMENT