Skip to content

Instantly share code, notes, and snippets.

@abezhinaru
Last active February 2, 2018 06:25
Show Gist options
  • Save abezhinaru/664e27922580910983d1 to your computer and use it in GitHub Desktop.
Save abezhinaru/664e27922580910983d1 to your computer and use it in GitHub Desktop.
window.test = tape;
// Concatenative inheritance example;
var Animal = {
animalName: 'animal',
describe: function() {
return 'This is an ' + this.animalName + '. ';
}
};
var ElephantFactory = (function() {
// default properties of elephant
var weight = {weight: 2000};
var animalName = {animalName: 'elephant'};
var elephant = Object.create(Animal);
// mutators of private fields
elephant.getWeight = function() {
return weight.weight;
};
elephant.setWeight = function(weight) {
weight.weight = weight;
};
// @overridden method
elephant.describe = function () {
return Animal.describe.call(this)
+ 'Which weighs is '
+ elephant.getWeight();
}
// Create closure for separating private fields from free access
return function(options) {
weight.weight = options.weight || weight.weight;
delete options.weight;
return Object.assign(elephant, animalName, options);
}
})();
test('check inheritance', function(assert) {
var elephantWeight = 5000;
var expectedDescribe = 'This is an elephant. Which weighs is ' + elephantWeight;
var elephant = ElephantFactory({
animalName: 'elephant',
weight: elephantWeight
});
assert.equal(elephant.weight, undefined);
assert.equal(elephant.getWeight(), elephantWeight);
assert.equal(elephant.describe(), expectedDescribe);
assert.end();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment