Last active
August 29, 2015 14:23
-
-
Save hwclass/2605850f15e7e05a92d4 to your computer and use it in GitHub Desktop.
Mixin pattern example in Javascript.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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