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
AWS⼊社という選択肢、⾒えていますか
iwamot
2
1.1k
Terraform未経験の御様に対してどの ように導⼊を進めていったか
tkikuchi
2
390
TypeScript、上達の瞬間
sadnessojisan
43
12k
Exadata Database Service on Dedicated Infrastructure(ExaDB-D) UI スクリーン・キャプチャ集
oracle4engineer
PRO
2
3.1k
TinyGoを使ったVSCode拡張機能実装
askua
2
210
OCI 運用監視サービス 概要
oracle4engineer
PRO
0
4.7k
인디 앱 개발자와 Flutter
tinyjin
0
160
テストコード品質を高めるためにMutation Testingライブラリ・Strykerを実戦導入してみた話
ysknsid25
6
2.3k
CysharpのOSS群から見るModern C#の現在地
neuecc
0
460
Terraform CI/CD パイプラインにおける AWS CodeCommit の代替手段
hiyanger
1
200
mikroBus HAT を用いた簡易ベアメタル開発
tarotene
0
330
QAEチームが辿った3年 ボクらが改善業務にスクラムを選んだワケ / 20241108_cloudsign_ques23
bengo4com
0
1.4k
Featured
See All Featured
CoffeeScript is Beautiful & I Never Want to Write Plain JavaScript Again
sstephenson
159
15k
No one is an island. Learnings from fostering a developers community.
thoeni
19
3k
Keith and Marios Guide to Fast Websites
keithpitt
409
22k
Ruby is Unlike a Banana
tanoku
96
11k
How GitHub (no longer) Works
holman
310
140k
How to train your dragon (web standard)
notwaldorf
88
5.7k
The World Runs on Bad Software
bkeepers
PRO
65
11k
JavaScript: Past, Present, and Future - NDC Porto 2020
reverentgeek
47
5k
Visualization
eitanlees
145
15k
Raft: Consensus for Rubyists
vanstee
136
6.6k
Helping Users Find Their Own Way: Creating Modern Search Experiences
danielanewman
29
2.3k
Rails Girls Zürich Keynote
gr2m
94
13k
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