Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Javascript Best Practices
Search
Jonathan Alvarez
March 19, 2015
Technology
3
340
Javascript Best Practices
Talk about some Javascript features that should be used with caution.
Jonathan Alvarez
March 19, 2015
Tweet
Share
Other Decks in Technology
See All in Technology
普通のエンジニアがLaravelコアチームメンバーになるまで
avosalmon
0
670
I could be Wrong!! - Learning from Agile Experts
kawaguti
PRO
8
2.5k
Azureの開発で辛いところ
re3turn
0
200
終了の危機にあった15年続くWebサービスを全力で存続させる - phpcon2024
yositosi
28
25k
Opcodeを読んでいたら何故かphp-srcを読んでいた話
murashotaro
0
360
開発生産性向上! 育成を「改善」と捉えるエンジニア育成戦略
shoota
2
830
Formal Development of Operating Systems in Rust
riru
1
380
DUSt3R, MASt3R, MASt3R-SfM にみる3D基盤モデル
spatial_ai_network
3
500
React Routerで実現する型安全なSPAルーティング
sansantech
PRO
4
880
OPENLOGI Company Profile for engineer
hr01
1
17k
スケールし続ける事業とサービスを支える組織とアーキテクチャの生き残り戦略 / The survival strategy for Money Forward’s engineering.
moneyforward
0
240
Unlearn Product Development - Unleashed Edition
lemiorhan
PRO
2
170
Featured
See All Featured
The World Runs on Bad Software
bkeepers
PRO
66
11k
Into the Great Unknown - MozCon
thekraken
34
1.6k
Making the Leap to Tech Lead
cromwellryan
133
9k
GraphQLの誤解/rethinking-graphql
sonatard
68
10k
Building Flexible Design Systems
yeseniaperezcruz
328
38k
The Art of Delivering Value - GDevCon NA Keynote
reverentgeek
8
1.2k
Java REST API Framework Comparison - PWX 2021
mraible
28
8.3k
Why You Should Never Use an ORM
jnunemaker
PRO
54
9.1k
Bash Introduction
62gerente
609
210k
RailsConf 2023
tenderlove
29
960
Code Reviewing Like a Champion
maltzj
521
39k
A Philosophy of Restraint
colly
203
16k
Transcript
BUENAS PRÁCTICAS! EN JAVASCRIPT @jonalvarezz
'5' - 3
'5' - 3 2
'5' + + '5'
'5' + + '5' '55'
'foo' + + 'foo'
'foo' + + 'foo' 'fooNaN'
'5' + - + - - + - - +
+ - + - + - - - '3'
'5' + - + - - + - - +
+ - + - + - - - '3' '53'
QUE GONORREA OME
BMBOHVBHF XJUIFOPSNPVT expresiveQPXFS Douglas Crockford
Semicolon Except: for, function, if, switch, try and while
return! {! status: true! };
return {! status: true! };
None
Floating Points
0.1 + 0.2
0.1 + 0.2 0.30000000000000004 IEEE Standard for Binary Floating-Point Arithmetic
0.1 + 0.2 0.30000000000000004 IEEE Standard for Binary Floating-Point Arithmetic
Use integer arithmetic in floating
(0.1 + 0.2)*100 30/100 0.3
parseInt()
parseInt("43");! // 43! ! parseInt("43 km");! // 43!
parseInt("09");! // 0! ! parseInt("09", 10);! // 9!
parseInt("09");! // 0! ! parseInt("09", 10);! // 9!
parseInt("09");! // 0! ! parseInt("09", 10);! // 9!
parseInt("09");! // 0! ! parseInt("09", 10);! // 9!
parseInt("09");! // 0! ! parseInt("09", 10);! // 9! "-8":464&5)&3"%*9 1"3".&5&3
Equality Operators == and !=
'' == '0'! // false! ! 0 == '0'! //
true! !
false == 'false'! // false! ! false == '0'! //
true! !
'\t\r\n ' == 0! // true! !
'\t\r\n ' == 0! // true! !
None
None
Use! === and !==
NaN Not a Number
+ "0";! // 0! ! ! + "foo";! // NaN
NaN === NaN! // false! ! ! NaN !== NaN!
// true
isNaN("I'm a NaN")! // true
*DPVMECFBCFUUFS QSPHSBNNFSCZVTJOH POMZUIFgood partsBOE BWPJEJOHUIFbad parts Douglas Crockford
Global variables The worst JavaScript's feature
var foo = "value";! ! var moo = function() {!
! foo = "Change global";! } // Place a var statement! // outside of any function
window.foo = "value"; // Defining direct to global object
foo = "value"; // Implied global
"-8":464& VARIABLE STATEMENT var foo = "value";
var moo = function() {! ! ! var foo =
"yay!";! ! // ....! ! } // The right way
var MYAPP = {};! ! var MYAPP.author = {! "first-name"
: "Jonathan",! "username" : "@jonalvarezz"! };! ! var MYAPP.speakAt = {! "name" : "PereiraJS",! "location" : "Pereira"! };! // Need globals?
#&55&3
Functions
function moo() {! ! } // function statement var moo
= function() {! ! } // function expression
moo(); // Prints "foo"! ! function moo() {! ! console.log("foo");!
} // function statement! // defined at run-time
moo(); // Error! ! var moo = function() {! !
console.log("foo");! } // function expression! // defined at parse-time
if (true) { ! ! function foo() {! ! !
return 1;! }! }! else { ! ! function foo() {! ! ! return 2;! }! }! ! foo(); ! // function statement
if (true) { ! ! function foo() {! ! !
return 1;! }! }! else { ! ! function foo() {! ! ! return 2;! }! }! ! foo(); // 2 ! // function statement
var foo;! ! if (true) { ! ! foo =
function() {! ! ! return 1;! }! }! else { ! ! foo = function() {! ! ! return 2;! }! }! ! foo(); // 1 ! // function expression
var myMath = {! ! add: function( a, b )
{! ! ! return a + b;! ! }! };! ! myMath.add( 2, 4 ); // 6 ! // function expression
'*345$-"44'6/$5*0/4 A function is an "Object"
'*345$-"44'6/$5*0/4 A function is an "Object"
var moo = function() {! ! // yay!! } //
function expression
(function() {! ! ! ! 'use strict';! ! ! var
a, b, c;! ! ! // ...! ! })(); // function statement! // Anonymous function! // Self invoked function
Scope
! var foo = function() {! ! var a =
3, b = 5;! ! ! var bar = function() {! ! ! var b = 7, c = 11;! ! ! ! a += b + c;! ! };! ! ! bar();! ! ! return a;! }
! var foo = function() {! ! var a =
3, b = 5;! ! ! var bar = function() {! ! ! var b = 7, c = 11;! ! ! ! a += b + c;! ! };! ! ! bar();! ! ! return a;! } // 21
! var foo = function() {! ! var a =
3, b = 5;! ! ! var bar = function() {! ! ! var b = 7, c = 11;! ! ! ! a += b + c;! ! };! ! ! bar();! ! ! return a;! } // 21 Function Scope!
Closures
* CODES *
Modules
"NPEVMFJTBGVODUJPO PSPCKFDUUIBUQSFTFOUT BOEJOUFSGBDFCVUUIBU hides its state and implementation
var seria_maker = function() {! ! var prefix = '';!
! var seq = 0;! ! return {! ! ! set_prefix: function(p) {! ! ! ! prefix = String(p);! ! ! },! ! ! set_seq: function(s) {! ! ! ! seq = s;! ! ! },! ! ! gensym: function() {! ! ! ! var result = prefix + seq;! ! ! ! seq += 1;! ! ! ! return result;! ! ! }! ! };! }; // Object that generates uniques strings
var sm = seria_maker();! ! sm.set_prefix('Q');! ! sm.set_seq(1000);! ! var
unique = sm.gensym();! ! // Q1000 // Object that generates uniques strings
jQuery
jQuery
None
%PZPV3&"--: OFFEK2VFSZ
:PVNJHIUOPU OFFEK2VFSZ
None
None
5IBOLZPV @jonalvarezz