Skip to content

Instantly share code, notes, and snippets.

@sinovic
Created January 9, 2018 15:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sinovic/9839f4fdda4ed464823716af5be66fc3 to your computer and use it in GitHub Desktop.
Save sinovic/9839f4fdda4ed464823716af5be66fc3 to your computer and use it in GitHub Desktop.
The mixin pattern // source http://jsbin.com/yeqiyo
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<script src="https://jashkenas.github.io/underscore/underscore-min.js"></script>
<title>The mixin pattern</title>
</head>
<body>
<script id="jsbin-javascript">
//=========== The mixin pattern Mixins ===========
// help in significantly reducing functional repetition
// in our code and help in function reuse.
// Shared functionality encapsulated into a CustomLogger
var logger = (function() {
return {
log: function(message) {
console.log(message);
}
}
})();
// An object that will need the custom logger
// to log system specific logs
var Server = (function(Logger) {
var CustomServer = function () {
this.init = function () {
this.log('Initializing Server...');
};
};
// This copies/extends the members of the 'CustomLogger'
// into 'CustomServer'
_.extend(CustomServer.prototype, Logger);
return CustomServer;
})(logger);
(new Server())().init();
</script>
<script id="jsbin-source-javascript" type="text/javascript">//=========== The mixin pattern Mixins ===========
// help in significantly reducing functional repetition
// in our code and help in function reuse.
// Shared functionality encapsulated into a CustomLogger
var logger = (function() {
return {
log: function(message) {
console.log(message);
}
}
})();
// An object that will need the custom logger
// to log system specific logs
var Server = (function(Logger) {
var CustomServer = function () {
this.init = function () {
this.log('Initializing Server...');
};
};
// This copies/extends the members of the 'CustomLogger'
// into 'CustomServer'
_.extend(CustomServer.prototype, Logger);
return CustomServer;
})(logger);
(new Server())().init();</script></body>
</html>
//=========== The mixin pattern Mixins ===========
// help in significantly reducing functional repetition
// in our code and help in function reuse.
// Shared functionality encapsulated into a CustomLogger
var logger = (function() {
return {
log: function(message) {
console.log(message);
}
}
})();
// An object that will need the custom logger
// to log system specific logs
var Server = (function(Logger) {
var CustomServer = function () {
this.init = function () {
this.log('Initializing Server...');
};
};
// This copies/extends the members of the 'CustomLogger'
// into 'CustomServer'
_.extend(CustomServer.prototype, Logger);
return CustomServer;
})(logger);
(new Server())().init();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment