Skip to content

Instantly share code, notes, and snippets.

@hwclass
Last active August 29, 2015 14:23
Show Gist options
  • Save hwclass/2605850f15e7e05a92d4 to your computer and use it in GitHub Desktop.
Save hwclass/2605850f15e7e05a92d4 to your computer and use it in GitHub Desktop.
Mixin pattern example in Javascript.
var Person = function (name, age) {
this.name = name;
this.age = age;
}
function extend(destination, source) {
for (var k in source) {
if (source.hasOwnProperty(k)) {
destination[k] = source[k];
}
}
return destination;
}
extend(Person.prototype, mixin);
var person = new Person('Baris', 13)
person.getName();
person.getAge();
var Person2 = function (name, age) {
this.name = name;
this.age = age;
}
var mixin2 = function () {
this.getName = function () {
return this.name;
};
this.getAge = function () {
return this.age;
}
}
mixin2.call(Person2.prototype);
var person2 = new Person2('Takinardi', 12);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment