-
Notifications
You must be signed in to change notification settings - Fork 142
Update 2009-03-06-javascript-best-practices.md #635
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -522,8 +522,8 @@ The other problem of nesting is variable names and loops. As you normally start | |
|
||
function renderProfiles(o) { | ||
var out = document.getElementById('profiles'); | ||
var ul = document.createElement('ul'); | ||
for(var i = 0; i < o.members.length; i++) { | ||
var ul = document.createElement('ul'); | ||
var li = document.createElement('li'); | ||
li.appendChild(document.createTextNode(o.members[i].name)); | ||
var nestedul = document.createElement('ul'); | ||
|
@@ -538,6 +538,7 @@ The other problem of nesting is variable names and loops. As you normally start | |
nestedul.appendChild(datali); | ||
} | ||
li.appendChild(nestedul); | ||
ul.appendChild(li); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Previously had no attachment of the generated li node to the 'enclosing' ul node. |
||
} | ||
out.appendChild(ul); | ||
} | ||
|
@@ -546,26 +547,27 @@ As I am using the generic — really throw-away — variable names `ul` and `li` | |
|
||
function renderProfiles(o) { | ||
var out = document.getElementById('profiles'); | ||
var ul = document.createElement('ul'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as line 525. ul node only created once. |
||
for(var i = 0; i < o.members.length; i++) { | ||
var ul = document.createElement('ul'); | ||
var li = document.createElement('li'); | ||
li.appendChild(document.createTextNode(data.members[i].name)); | ||
li.appendChild(addMemberData(o.members[i])); | ||
li.appendChild(document.createTextNode(o.members[i].name)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Typo: |
||
li.appendChild(addMemberData(o.members[i].data)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Information hiding and access optimisation (q.v. avoid excessive member access implied by Optimize loops). |
||
ul.appendChild(li); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as 541, attach new li node to enclosing ul node. |
||
} | ||
out.appendChild(ul); | ||
} | ||
function addMemberData(member) { | ||
function addMemberData(data) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Changes to reflect that only the Note: this Actually, on the subject of better names, perhaps the function name "addMemberData" is a bit vague. Add to what? It isn't a member function so there is no invoking object to give context. Perhaps a better name would be "buildMemberDataList" since this is what it actually does. Again, left it as is to minimise changes. |
||
var ul = document.createElement('ul'); | ||
for(var i = 0; i < member.data.length; i++) { | ||
for(var i = 0; i < data.length; i++) { | ||
var li = document.createElement('li'); | ||
li.appendChild( | ||
document.createTextNode( | ||
member.data[i].label + ' ' + | ||
member.data[i].value | ||
data[i].label + ' ' + | ||
data[i].value | ||
) | ||
); | ||
ul.appendChild(li); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Having |
||
} | ||
ul.appendChild(li); | ||
return ul; | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Moved assignment of
ul
out of loop so not reassigned for every element ofo.members
accessed (thereby throwing away all but the last into the garbage collector).