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
320
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
AOAI をきっかけに 社内の Azure 管理を見直した話
recruitengineers
PRO
1
450
今年のRubyKaigiはProfiler Year🤘
osyoyu
0
360
生成AIの変革の時代に、直近1年で直面した課題とその解決策
ktc_wada
0
660
Android Target SDK 35 (Android 15) 対応の概要
akkie76
0
160
一生覚えておきたい「システム開発=コミュニケーション」〜初めての実務案件振り返りLT〜
maimyyym
2
320
require(ESM)とECMAScript仕様
uhyo
4
980
MLOpsの「壁」を乗り越える、LINEヤフーの Data Quality as Code
lycorptech_jp
PRO
8
620
Azureの基本的な権限管理の勉強会
yhana
1
2.1k
M&A戦略を支えるデータマネジメント (MIDAS Tech Study #16 GENDA Komiyama)
kommy339
1
110
M5と自作基板をくっつけてみた〜M5 Japan Tour 2024 Spring 福冈 (Fukuoka|福岡)〜
keropiyo
0
130
LangSmith入門―トレース/評価/プロンプト管理などを担うLLMアプリ開発プラットフォーム
os1ma
5
710
「スニダン」開発組織の構造に込めた意図 ~組織作りはパッションや政治ではない!~
rinchsan
4
610
Featured
See All Featured
Designing the Hi-DPI Web
ddemaree
276
33k
CoffeeScript is Beautiful & I Never Want to Write Plain JavaScript Again
sstephenson
155
14k
JavaScript: Past, Present, and Future - NDC Porto 2020
reverentgeek
41
4.4k
Building a Scalable Design System with Sketch
lauravandoore
457
32k
Fashionably flexible responsive web design (full day workshop)
malarkey
398
65k
Pencils Down: Stop Designing & Start Developing
hursman
117
11k
The Cost Of JavaScript in 2023
addyosmani
20
3.9k
Web development in the modern age
philhawksworth
203
10k
What the flash - Photography Introduction
edds
64
11k
RailsConf 2023
tenderlove
8
550
[Rails World 2023 - Day 1 Closing Keynote] - The Magic of Rails
eileencodes
8
1.3k
Reflections from 52 weeks, 52 projects
jeffersonlam
345
19k
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