SlideShare ist ein Scribd-Unternehmen logo
1 von 10
Downloaden Sie, um offline zu lesen
7
Javascript Hacks for Hipsters
/Tal Bereznitskey @ketacode
Hipster Hack #1 - Method
calling
Trade bloated if/else blocks for a quick function call
//Boring
if(success){
obj.start();
}else{
obj.stop();
}
//Hipster-fun
varmethod=(success?'start':'stop');
obj[method]();
Hipster Hack #2 - String joins
Concatenate two strings or more simpler, easier and faster
['first','name'].join('');//='firstname';
['milk','coffee','sugar'].join(',');//='milk,coffee,sugar'
Hipster Hack #3 - Default
Operator ||
Use the || operator to use the second argument if the first is falsy
//defaultto'Noname'whenmyNameisempty(ornull,orundefined)
varname=myName||'Noname';
//makesurewehaveanoptionsobject
vardoStuff=function(options){
options=options||{};
//...
};
Hipster Hack #4 - Guard
Operator &&
Use the && operator to eliminate almost all IF calls and make your code
nicers
//Boring
if(isThisAwesome){
alert('yes');//it'snot
}
//Awesome
isThisAwesome&&alert('yes');
//Alsocoolforguardingyourcode
varaCoolFunction=undefined;
aCoolFunction&&aCoolFunction();//won'trunnorcrash
Hipster Hack #5 - XXX Operator
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.
varz=15;
doSomeMath(z,10);
xxx//Greatplaceholder.I'mtheonlyoneusingxxxandit'ssoeasytofindincodeinsteadofT
doSomeMoreMath(z,15);
Hipster Hack #6 - Timing
Use the console’s timing methods to test for event-loop blocking code
vara=[1,2,3,4,5,6,7,8,9,10];
console.time('testing_forward');
for(vari=0;i<a.length;i++);
console.timeEnd('testing_forward');
//output:testing_forward:0.041ms
console.time('testing_backwards');
for(vari=a.length-1;i>=0;i--);
console.timeEnd('testing_backwards');
//output:testing_backwards:0.030ms
Hipster Hack #7 - Debugging
Drop a debugger statement and the debugger will stop on that line
varx=1;
debugger;//Codeexecutionstopshere,happydebugging
x++;
varx=Math.random(2);
if(x>0.5){
debugger;//Conditionalbreakpoint
}
x--;
More...
There are 2 more hipster hacks in the full blog post:
Javascript Hacks for Hipsters
Happy Coding
Tal Bereznitskey
Blog: berzniz.com
Twitter: @ketacode

Weitere ähnliche Inhalte

Was ist angesagt?

Good Evils In Perl
Good Evils In PerlGood Evils In Perl
Good Evils In Perl
Kang-min Liu
 
ECMAScript2015
ECMAScript2015ECMAScript2015
ECMAScript2015
qmmr
 

Was ist angesagt? (20)

2013-06-15 - Software Craftsmanship mit JavaScript
2013-06-15 - Software Craftsmanship mit JavaScript2013-06-15 - Software Craftsmanship mit JavaScript
2013-06-15 - Software Craftsmanship mit JavaScript
 
There's a Filter For That
There's a Filter For ThatThere's a Filter For That
There's a Filter For That
 
Class 7a: Functions
Class 7a: FunctionsClass 7a: Functions
Class 7a: Functions
 
Ruby and Rails by example
Ruby and Rails by exampleRuby and Rails by example
Ruby and Rails by example
 
Advanced Shell Scripting
Advanced Shell ScriptingAdvanced Shell Scripting
Advanced Shell Scripting
 
Good Evils In Perl
Good Evils In PerlGood Evils In Perl
Good Evils In Perl
 
De 0 a 100 con Bash Shell Scripting y AWK
De 0 a 100 con Bash Shell Scripting y AWKDe 0 a 100 con Bash Shell Scripting y AWK
De 0 a 100 con Bash Shell Scripting y AWK
 
Ruby - Uma Introdução
Ruby - Uma IntroduçãoRuby - Uma Introdução
Ruby - Uma Introdução
 
An introduction to Ruby
An introduction to RubyAn introduction to Ruby
An introduction to Ruby
 
Vim Hacks
Vim HacksVim Hacks
Vim Hacks
 
Fancy talk
Fancy talkFancy talk
Fancy talk
 
Introduction to CoffeeScript
Introduction to CoffeeScriptIntroduction to CoffeeScript
Introduction to CoffeeScript
 
Ruby Programming Language
Ruby Programming LanguageRuby Programming Language
Ruby Programming Language
 
Php radomize
Php radomizePhp radomize
Php radomize
 
Background Processing in Ruby on Rails
Background Processing in Ruby on RailsBackground Processing in Ruby on Rails
Background Processing in Ruby on Rails
 
Ruby on Rails Presentation
Ruby on Rails PresentationRuby on Rails Presentation
Ruby on Rails Presentation
 
ECMAScript2015
ECMAScript2015ECMAScript2015
ECMAScript2015
 
JavaScript Design Patterns
JavaScript Design PatternsJavaScript Design Patterns
JavaScript Design Patterns
 
파이썬 플라스크로 배우는 웹프로그래밍 #2 (ABCD)
파이썬 플라스크로 배우는 웹프로그래밍 #2 (ABCD)파이썬 플라스크로 배우는 웹프로그래밍 #2 (ABCD)
파이썬 플라스크로 배우는 웹프로그래밍 #2 (ABCD)
 
Introdução ao Perl 6
Introdução ao Perl 6Introdução ao Perl 6
Introdução ao Perl 6
 

Andere mochten auch

IT Project Tracking and Prioritization - Kickoff
IT Project Tracking and Prioritization - KickoffIT Project Tracking and Prioritization - Kickoff
IT Project Tracking and Prioritization - Kickoff
Margaret D.
 
Lyn pugecte bereket folder
Lyn pugecte  bereket folderLyn pugecte  bereket folder
Lyn pugecte bereket folder
breket
 

Andere mochten auch (19)

Making your Angular.js Application accessible
Making your Angular.js Application accessibleMaking your Angular.js Application accessible
Making your Angular.js Application accessible
 
AEJMC 2014 - How scientists see engagement goals
AEJMC 2014 - How scientists see engagement goalsAEJMC 2014 - How scientists see engagement goals
AEJMC 2014 - How scientists see engagement goals
 
Smart goals
Smart goalsSmart goals
Smart goals
 
Goal setting and prioritizing
Goal setting and prioritizingGoal setting and prioritizing
Goal setting and prioritizing
 
Gartner Report: Aligning Supply and Demand for IT Services
Gartner Report: Aligning Supply and Demand for IT ServicesGartner Report: Aligning Supply and Demand for IT Services
Gartner Report: Aligning Supply and Demand for IT Services
 
CCI Project Prioritization Project
CCI Project Prioritization ProjectCCI Project Prioritization Project
CCI Project Prioritization Project
 
Smart Goals Presentation
Smart Goals PresentationSmart Goals Presentation
Smart Goals Presentation
 
Project Prioritization Tool
Project Prioritization ToolProject Prioritization Tool
Project Prioritization Tool
 
Prioritising almost anything – UXcamp London
Prioritising almost anything – UXcamp LondonPrioritising almost anything – UXcamp London
Prioritising almost anything – UXcamp London
 
IT Project Tracking and Prioritization - Kickoff
IT Project Tracking and Prioritization - KickoffIT Project Tracking and Prioritization - Kickoff
IT Project Tracking and Prioritization - Kickoff
 
Easy Project Prioritization
Easy Project PrioritizationEasy Project Prioritization
Easy Project Prioritization
 
The Javascript Toolkit 2.0
The Javascript Toolkit 2.0The Javascript Toolkit 2.0
The Javascript Toolkit 2.0
 
Roadmaps That Inspire
Roadmaps That InspireRoadmaps That Inspire
Roadmaps That Inspire
 
Utilizando recursos de realidade aumentada em aplicações
Utilizando recursos de realidade aumentada em aplicaçõesUtilizando recursos de realidade aumentada em aplicações
Utilizando recursos de realidade aumentada em aplicações
 
Lyn pugecte bereket folder
Lyn pugecte  bereket folderLyn pugecte  bereket folder
Lyn pugecte bereket folder
 
Audience
AudienceAudience
Audience
 
Realidade Virtual/Aumentada e Mixed Reality. Estamos no Futuro?
Realidade Virtual/Aumentada e Mixed Reality. Estamos no Futuro?Realidade Virtual/Aumentada e Mixed Reality. Estamos no Futuro?
Realidade Virtual/Aumentada e Mixed Reality. Estamos no Futuro?
 
The Host opening analysis
The Host opening analysisThe Host opening analysis
The Host opening analysis
 
Stats joueurs
Stats joueursStats joueurs
Stats joueurs
 

Kürzlich hochgeladen

CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female serviceCALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
anilsa9823
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
mohitmore19
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
Health
 

Kürzlich hochgeladen (20)

CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female serviceCALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
 
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AISyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
 
Diamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionDiamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with Precision
 
Microsoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdfMicrosoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdf
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.com
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
 
Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptx
 
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docx
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Models
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTV
 
How To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsHow To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.js
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial Goals
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
 
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
 

JavaScript Hacks for Hipsters