{ "title": "JavaScript Best Practices and Common Pitfalls", "sections": [ { "title": "Memory Management", "content": [ { "type": "point", "text": "Avoid creating reference loops between DOM objects and JavaScript functions." }, { "type": "example", "code": "function attachEvents() { var element = document.getElementById('myID'); element.onclick = function() { delete element; alert('Element clicked'); }; } attachEvents();" } ] }, { "title": "Number Types", "content": [ { "type": "point", "text": "JavaScript treats all numbers as floating-point numbers." }, { "type": "example", "code": "var myNumber = 3.5; var myResult = 3.5 + 1; // Result is 4.5, as expected" } ] }, { "title": "Using 'with' for Context Injection", "content": [ { "type": "point", "text": "Use 'with' for context injection only when necessary." }, { "type": "example", "code": "team.attackers.myWarrior = { attack: 1, speed: 3, magic: 5}; var sc = team.attackers.myWarrior; console.log('Your warrior power is ' + (sc.attack * sc.speed));" } ] }, { "title": "Using 'setTimeout' and 'setInterval'", "content": [ { "type": "point", "text": "Pass functions themselves to 'setTimeout' and 'setInterval' instead of strings." }, { "type": "example", "code": "function log1() { console.log(document.location); } function log2(arg) { console.log(arg); } var myValue = \"test\"; setTimeout(log1, 100);" } ] } ] }