Created
January 9, 2018 15:26
-
-
Save sinovic/9839f4fdda4ed464823716af5be66fc3 to your computer and use it in GitHub Desktop.
The mixin pattern // source http://jsbin.com/yeqiyo
This file contains hidden or 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
<!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> |
This file contains hidden or 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
//=========== 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