What's different about writing a Javascript code in the following ways:
Original Form:
'use strict';
var openCtrl = document.getElementById('btn-search'),
closeCtrl = document.getElementById('btn-search-close'),
searchContainer = document.querySelector('.search'),
inputSearch = searchContainer.querySelector('.search__input');
function init() {
initEvents();
}
function initEvents() {
openCtrl.addEventListener('click', openSearch);
closeCtrl.addEventListener('click', closeSearch);
document.addEventListener('keyup', function(ev) {
// escape key.
if( ev.keyCode == 27 ) {
closeSearch();
}
});
}
function openSearch() {
searchContainer.classList.add('search--open');
inputSearch.focus();
}
function closeSearch() {
searchContainer.classList.remove('search--open');
inputSearch.blur();
inputSearch.value = '';
}
init();
Now what's the difference to do this:
;(function(window) {
'use strict';
var openCtrl = document.getElementById('btn-search'),
closeCtrl = document.getElementById('btn-search-close'),
searchContainer = document.querySelector('.search'),
inputSearch = searchContainer.querySelector('.search__input');
function init() {
initEvents();
}
function initEvents() {
openCtrl.addEventListener('click', openSearch);
closeCtrl.addEventListener('click', closeSearch);
document.addEventListener('keyup', function(ev) {
// escape key.
if( ev.keyCode == 27 ) {
closeSearch();
}
});
}
function openSearch() {
searchContainer.classList.add('search--open');
inputSearch.focus();
}
function closeSearch() {
searchContainer.classList.remove('search--open');
inputSearch.blur();
inputSearch.value = '';
}
init();
})(window);