Skip to content

Instantly share code, notes, and snippets.

@jackw
Created October 15, 2013 07:23
Show Gist options
  • Save jackw/6987803 to your computer and use it in GitHub Desktop.
Save jackw/6987803 to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8 />
<title>JS Bin</title>
</head>
<body>
</body>
</html>
function forEachIn(object, action) {
for (var property in object) {
if (Object.prototype.hasOwnProperty.call(object, property)) {
action(property, object[property]);
}
}
}
function Dictionary (startValues) {
this.values = startValues || {};
}
Dictionary.prototype.store = function (name, value) {
this.values[name] = value;
};
Dictionary.prototype.lookup = function (name) {
return this.values[name];
};
Dictionary.prototype.contains = function (name) {
console.log(this.values);
return Object.prototype.propertyIsEnumerable.call(this.values, name);
};
Dictionary.prototype.each = function (action) {
forEachIn(this.values, action);
};
var colours = new Dictionary ({
Grover : 'blu',
Elmo : 'rosso',
Bert: 'gialo'
});
console.log(colours.contains("Grover"));
colours.store("Ernie", "arancione");
colours.each(function (name, colour) {
console.log(name + " è " + colour);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment