SteelToe is a small javascript snippet designed to solve the problem of access to undefined object properties. This is especially important when referring to nested properties. It also realized the possibility of autovivification, which in order simplifies the creation of the desired properties. In general - quite useful sugar in OOP.var object = { info: { name: { first: 'Jonathan', last: 'Clem' } } } steelToe(object)('info')('name')('last')(); // 'Clem' steelToe(object)('info')('features')('hairColor')(); // undefined  var object = { info: { name: { first: 'Jonathan', last: 'Clem' } } } steelToe(object).walk('info.name.last'); // 'Clem' steelToe(object).walk('info.features.hairColor'); // undefined  var jonathan = { info: { name: { first: 'Jonathan', last: 'Clem' } } }, steelToe(jonathan).set('info.name.middle', 'Tyler'); steelToe(jonathan).set('info.favorites.movie', 'Harold & Maude'); jonathan.info.name.middle; // Tyler jonathan.info.favorites.movie; // Harold & Maude  var fatherFirstNames = []; for (var i = 0; i < families.length; i ++) { var first = families[i].father.info.name.first; if (first) { fatherFirstNames.push(first); } } // TypeError: 'undefined' is not an object (evaluating 'family.father.info.name.first') TypeError , since nothing guarantees that the father property is defined, or it has the properties you require. Usually, to prevent errors of this kind, we write wretchedness, like this var farherFirstNames = []; for (var i = 0; i < families.length; i++) { var father = families[i].father; if (father && father.info && father.info.name && father.info.name.first) { fatherFirstNames.push(father.info.name.first); } }  var fatherFirstNames = []; for (var i = 0; i < families.length; i++) { var name = steelToe(families[i]).walk('father.info.name.first'); if (name) { fatherFirstNames.push(name); } } fatherFirstNames; // ["Hank", "Dale", "Bill"] TypeError s! Happiness!Source: https://habr.com/ru/post/159269/
All Articles