Skip to content

Instantly share code, notes, and snippets.

@dtateii
Created February 25, 2016 17:58
Show Gist options
  • Save dtateii/a213f2b46a081cf05f14 to your computer and use it in GitHub Desktop.
Save dtateii/a213f2b46a081cf05f14 to your computer and use it in GitHub Desktop.
Example JS Number Properties
<!DOCTYPE html>
<html>
<head>
<title>JS Test</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js"></script>
</head>
<body>
<header>
<h1>JS Test</h1>
<p>Use console.</p>
</header>
<script>
var alphaprops = {}; // <- Object, not an array
var numberprops = {}; // <- Object, not an array
// Order of prop assignment disagrees with natural order implied by prop names.
alphaprops['three'] = "C";
alphaprops['two'] = "B";
alphaprops['one'] = "A";
// Order of prop assignment disagrees with natural order implied by prop names.
// Note prop names are strings not numbers.
numberprops['3'] = "C";
numberprops['2'] = "B";
numberprops['1'] = "A";
console.log("Alpha props");
// Yields props in the order assigned.
_.each( alphaprops, function(prop){
console.log(prop);
});
console.log("Numbers for props");
// Yields props reordered by numeric value of prop key, ascending.
_.each( numberprops, function(prop){
console.log(prop);
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment