Upgrade to Pro — share decks privately, control downloads, hide ads and more …

JS Tips

JS Tips

Avatar for Rodolfo Spalenza

Rodolfo Spalenza

October 12, 2015

More Decks by Rodolfo Spalenza

Other Decks in Programming

Transcript

  1. ~ $ jshint one_page_checkout.js one_page_checkout.js|56 col 4 warning 33| Missing

    semicolon. one_page_checkout.js|60 col 4 warning 33| Missing semicolon. one_page_checkout.js|64 col 4 warning 33| Missing semicolon. one_page_checkout.js|70 col 4 warning 33| Missing semicolon. one_page_checkout.js|73 col 26 warning 33| Missing semicolon. one_page_checkout.js|75 col 4 warning 33| Missing semicolon. one_page_checkout.js|82 col 4 warning 33| Missing semicolon. http://jshint.com/
  2. MINIMIZANDO GLOBAIS myGlobal = 'hello'; // antipattern console.log(myGlobal); // 'hello’

    console.log(window.myGlobal); // 'hello’ console.log(window["myGlobal"]); // 'hello’ console.log(this.myGlobal); // 'hello’
  3. GLOBAIS IMPLÍCITAS function sum(x, y) { // antipattern result =

    x + y; return result; } console.log(sum(1, 2)) // 3 console.log(result) // 3
  4. function sum(x, y) { var result = x + y;

    return result; } console.log(sum(1, 2)) // 3 console.log(result) // undefined MINIMIZANDO GLOBAIS
  5. OBJETO var obj = { first_name: 'Rodolfo', last_name: 'Spalenza', full_name:

    function() { return this.first_name + ' ' + this.last_name; } }; console.log(obj.first_name); // Rodolfo console.log(obj.full_name()); // Rodolfo Spalenza
  6. OBJETO var obj = { first_name: 'Rodolfo', last_name: 'Spalenza', full_name:

    function() { return this.first_name + ' ' + this.last_name; } }; obj.birth_year = 1988; console.log(obj.birth_year); // 1988 delete obj.birth_year; console.log(obj.birth_year); // undefined
  7. OBJETO COM CONSTRUTOR var obj = new Object(); obj.first_name =

    'Rodolfo'; console.log(obj.first_name); // 'Rodolfo'
  8. OBJETO COM CONSTRUTOR var obj = new Object('Rodolfo'); console.log(obj.constructor ===

    String); // true console.log(typeof obj.substring); // function
  9. OBJETO COM CONSTRUTOR var obj = new Object('Rodolfo'); console.log(obj.constructor ===

    String); // true console.log(typeof obj.substring); // function PODE LEVAR A RESULTADOS INESPERADOS
  10. FUNÇÕES CONSTRUTORAS var Person = function(name) { this.name = name;

    this.say = function() { return 'Hello! I am ' + this.name; }; }; var rodolfo = new Person('Rodolfo'); var rafael = new Person('Rafael'); console.log(rodolfo.say()); // 'Rodolfo' console.log(rafael.say()); // 'Rafael'
  11. FUNÇÕES CONSTRUTORAS var Person = function(name) { // cria um

    novo objeto usando o objeto literal // var this = {}; // adiciona propriedades e métodos ao this this.name = name; this.say = function() { return 'Hello! I am ' + this.name; }; // retorna this };
  12. FUNÇÕES CONSTRUTORAS var Person = function(name) { this.name = name;

    }; Person.prototype.say = function() { return 'Hello! I am ' + this.name; }; var rodolfo = new Person('Rodolfo'); var rafael = new Person('Rafael'); console.log(rodolfo.say()); // 'Rodolfo' console.log(rafael.say()); // 'Rafael'
  13. PROBLEMA DO NEW var Person = function(name) { this.name =

    name; }; Person('Rodolfo'); console.log(name); // 'Rodolfo' console.log(window.name); // 'Rodolfo'
  14. CONSTRUTOR AUTOINVOCÁVEL function Person(first_name, last_name) { this.first_name = first_name; this.last_name

    = last_name; return this; }; var person = Person('Rodolfo', 'Spalenza'); console.log(first_name); // 'Rodolfo' console.log(window.first_name); // 'Rodolfo' console.log(person.first_name); // 'Rodolfo'
  15. CONSTRUTOR AUTOINVOCÁVEL function Person(first_name, last_name) { if (!(this instanceof Person))

    { return new Person(first_name, last_name); } this.first_name = first_name; this.last_name = last_name; return this; }; var person = Person('Rodolfo', 'Spalenza'); console.log(first_name); // first_name is not defined console.log(window.first_name); // undefined console.log(person.first_name); // 'Rodolfo'
  16. FUNÇÕES NOMEADAS var sum = function sum(a, b) { return

    a + b; }; console.log(sum.name); // ’sum’
  17. FUNÇÕES ANÔNIMAS var sum = function(a, b) { return a

    + b; }; console.log(sum.name); // ’’
  18. DECLARAÇÕES DE FUNÇÕES function sum(a, b) { return a +

    b; } console.log(sum.name); // 'sum’
  19. FUNÇÕES IMEDIATAS var weekdayMsg = (function(date) { var days =

    ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'], msg = 'Weekday: ' + days[date.getDay()]; console.log(msg); return msg; }(new Date())); // Weekday: Friday console.log(weekdayMsg); // Weekday: Friday
  20. CALLBACKS var plus = function(a, b) { return a *

    b; }; var operation = function(a, b, callback) { if (typeof callback === 'function') { return callback(a, b); } else { return a + b; } }; operation(1, 2); // 3 operation(1, 2, plus); // 2
  21. NAMESPACES // 5 GLOBAIS // contrutores function Parent() {} function

    Child() {} // variáveis var someVar = 1; // objetos var firstModule = {}; firstModule.data = { a: 1, b: 2 }; var secondModule = {};
  22. NAMESPACES // 1 GLOBAL // objeto global var SMARTSYSTEM =

    {}; // contrutores SMARTSYSTEM.Parent = function() {}; SMARTSYSTEM.Child = function() {}; // variáveis SMARTSYSTEM.someVar = 1; // container de objetos SMARTSYSTEM.modules = {}; // objetos SMARTSYSTEM.modules.firstModule = {}; SMARTSYSTEM.modules.firstModule.data = { a: 1, b: 2 }; SMARTSYSTEM.modules.secondModule = {};
  23. MEMBROS PRIVADOS var person = { first_name: 'Rodolfo', last_name: 'Spalenza',

    full_name: function() { return this.first_name + ' ' + this.last_name; } }; console.log(person.first_name); // 'Rodolfo' console.log(person.full_name()); // 'Rodolfo Spalenza’
  24. MEMBROS PRIVADOS function Person() { this.first_name = 'Rodolfo'; this.last_name =

    'Spalenza'; this.full_name = function() { return this.first_name + ' ' + this.last_name; }; } var person = new Person(); console.log(person.first_name); // 'Rodolfo' console.log(person.full_name()); // 'Rodolfo Spalenza'
  25. MEMBROS PRIVADOS - CLOSURE function Person() { var first_name =

    'Rodolfo'; var last_name = 'Spalenza'; this.full_name = function() { return first_name + ' ' + last_name; }; } var person = new Person(); console.log(person.first_name); // undefined console.log(person.full_name()); // 'Rodolfo Spalenza'
  26. OBJETOS LITERAIS E PRIVACIDADE var person = (function() { var

    first_name = 'Rodolfo'; var last_name = 'Spalenza'; return { full_name: function() { return first_name + ' ' + last_name; } }; }()); console.log(person.first_name); // undefined console.log(person.full_name()); // 'Rodolfo Spalenza'
  27. PADRÃO MODULO var SMARTSYSTEM = SMARTSYSTEM || {}; SMARTSYSTEM.CreditCard =

    (function(globalRegex) { var globalRegex = globalRegex, masterRegex = '', visaRegex = ''; function isValid(number) {}; function brand(number) {}; return { isValid: isValid, brand: brand }; }(globalRegex)); console.log(SMARTSYSTEM.CreditCard.isValid('4111111111111111')); // true
  28. PADRÃO MODULO var SMARTSYSTEM = SMARTSYSTEM || {}; SMARTSYSTEM.CreditCard =

    (function(globalRegex) { var public = {}, mainRegex = globalRegex, masterRegex = '', visaRegex = ''; public.isValid = function(number) {}; public.brand = function(number) {}; return public; }(globalRegex)); console.log(SMARTSYSTEM.CreditCard.isValid('4111111111111111')); // true
  29. MODULOS CRIANDO CONSTRUTORES SMARTSYSTEM.CreditCard = (function(globalRegex) { var Constr, mainRegex

    = globalRegex, masterRegex = '', visaRegex = ''; Constr = function(n) { var number = n; function isValid() {} function brand() {} return { isValid: isValid, brand: brand }; }; return Constr; }(globalRegex));
  30. MODULOS CRIANDO CONSTRUTORES var cc01 = new SMARTSYSTEM.CreditCard('4539496648379164'); var cc02

    = new SMARTSYSTEM.CreditCard('5384319936559227'); console.log(cc01.brand()); // visa console.log(cc02.brand()); // master
  31. AJAX REQUEST $.ajax({ url: url, type: 'GET', dataType: 'json', data:

    { q: query }, error: function(data) {}, success: function(data) {} });
  32. HANDLEBARS <script id="entry-template" type="text/x-handlebars-template"> <div class="entry"> <h1>{{title}}</h1> <div class="body"> {{body}}

    </div> </div>V </script> <div id="box"></div> var source = $("#entry-template").html(); var template = Handlebars.compile(source); var context = { title: "My New Post", body: "My first post!" }; var html = template(context); $('#box').html(html);
  33. MEDIATOR var cc = new SMARTSYSTEM.CreditCard('4539496648379164'); var flag = cc.brand();

    SMARTSYSTEM.Mediator.publish('credit-card:active', flag); ... SMARTSYSTEM.Mediator.subscribe('credit-card:active', function(flag) { var identification = '#credit-card .brand.' + flag; $(identification).addClass('active'); });
  34. PLUGINS jQuery (function($, Handlebars, Mediator) { $.fn.creditCard = function(options) {

    if (!this.length) { return; } var self = this, settings = { brandClass: 'brand', activeClass: 'active', afterChange: function(data) {} }; // initialize settings settings = $.extend({}, settings, options); $(self).on('change', settings.brandClass, function(event) {}); // ... }; })(jQuery, Handlebars, SMARTSYSTEM.Mediator);
  35. PLUGINS jQuery $('#smartsystem-credit-card').creditCard({ brandClass: 'smartsystem-credit-card-brand', afterChange: function(data) { var cc

    = new SMARTSYSTEM.CreditCard(data), flag = cc.brand(); SMARTSYSTEM.Mediator.publish('credit-card:active', flag); } });
  36. CONVERSÃO DE TIPO IMPÍCITO false == 0 // true false

    === 0 // false '' == 0 // true '' === 0 // false