The document discusses techniques for writing clean JavaScript code. It provides examples of code smells and improvements to address issues like attaching events from the outside, separating selection from logic, shallow scope, overwriting default behavior, and separating logic from views. The document advocates for practices like modularizing functions, separating DOM manipulation from models, and creating model objects to represent DOM elements rather than directly manipulating the DOM. It encourages learning clean JavaScript techniques to write better structured and more maintainable code.
44. - Separate from Selector -
function showPhoto(photoId) {
var photo = document.getElementById(‘photo-’ + photoId);
// do ...
}
45. - Separate from Selector -
function showPhoto(photoId) {
var photo = document.getElementById(‘photo-’ + photoId);
// do ...
}
code smell
46. - Separate from Selector -
var showPhoto = function(element) {
// do ...
}
var photo = document.getElementById(‘photo-12’);
showPhoto(photo);
improvement
49. - modulized functions -
// users.js
var showUserName = function() {
// do somethig
}
var showUserAge = function(){
// do somethig
}
var validateUserForm = function() {
// do somethig
} code smell
50. - modulized functions -
// users.js
var showUserName = function() {
// do somethig
} global
var showUserAge = function(){
// do somethig
}
var validateUserForm = function() {
// do somethig
}
51. - modulized functions -
// users.js
var users = {};
global
users.showName = function () {
// do somethig
}
users.showAge = function (){
// do somethig
}
users.validateForm = function () {
// do somethig
}
52. - modulized functions -
// users.js
(function(global) {
global.users = {}; global
users.showName = function () {
// do somethig
}
// ...
)(this);
improvement
64. DOM - DOM to model -
var element = document.getElementById(‘message’);
element.addEventListener(‘click’, function() {
// this == element
if (this.getAttribute(‘data-is-checked’)) {
this.setAttribute(‘data-is-checked’, true);
this.innerText = ‘clicked!’;
}
});
code smell
65. DOM - DOM to model -
var domToModel = function(element, Model) {
var method, name, object, parent, proto;
model = Object.create(element);
proto = Model.prototype;
for (name in proto) {
method = proto[name];
model[name] = method;
}
Model.apply(model);
return model;
};
var CustomElement = function() {};
CustomElement.prototype.showText = function() {
if (!this.getAttribute(‘data-is-checked’)) { var element = document.getElementById(‘message’);
this.setAttribute(‘data-is-checked’, true); var model = domToModel(element, CustomElement);
this.innerText = ‘clicked!’; model.addEventListener(‘click’, function() {
} model.showText();
}; });
66. DOM - DOM to model -
var domToModel = function(element, Model) {
var method, name, object, parent, proto;
model = Object.create(element);
proto = Model.prototype;
for (name in proto) {
method = proto[name];
model[name] = method;
}
Model.apply(model);
return model;
};
var CustomElement = function() {};
CustomElement.prototype.showText = function() {
if (!this.getAttribute(‘data-is-checked’)) { var element = document.getElementById(‘message’);
this.setAttribute(‘data-is-checked’, true); var model = domToModel(element, CustomElement);
this.innerText = ‘clicked!’; model.addEventListener(‘click’, function() {
} model.showText();
}; });
67. DOM - DOM to model -
var domToModel = function(element, Model) {
var method, name, object, parent, proto;
model = Object.create(element);
proto = Model.prototype;
for (name in proto) {
method = proto[name];
model[name] = method;
}
Model.apply(model);
return model;
};
var CustomElement = function() {};
CustomElement.prototype.showText = function() {
if (!this.getAttribute(‘data-is-checked’)) { var element = document.getElementById(‘message’);
this.setAttribute(‘data-is-checked’, true); var model = domToModel(element, CustomElement);
this.innerText = ‘clicked!’; model.addEventListener(‘click’, function() {
} model.showText();
}; });
improvement