Skip to content

Instantly share code, notes, and snippets.

@sebastiankade
Created November 13, 2014 06:27
Show Gist options
  • Select an option

  • Save sebastiankade/3da3a3d08a772ca035ac to your computer and use it in GitHub Desktop.

Select an option

Save sebastiankade/3da3a3d08a772ca035ac to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>JS Bin</title>
</head>
<body>
<script id="jsbin-javascript">
(function () {
var initializing = false,
fnTest = /xyz/.test(function () {
xyz;
}) ? /\b_super\b/ : /.*/;
// The base Class implementation (does nothing)
this.Class = function () {};
// Create a new Class that inherits from this class
Class.extend = function (prop) {
var _super = this.prototype;
// Instantiate a base class (but only create the instance,
// don't run the init constructor)
initializing = true;
var prototype = new this();
initializing = false;
// Copy the properties over onto the new prototype
for (var name in prop) {
// Check if we're overwriting an existing function
prototype[name] = typeof prop[name] == "function" &&
typeof _super[name] == "function" && fnTest.test(prop[name]) ?
(function (name, fn) {
return function () {
var tmp = this._super;
// Add a new ._super() method that is the same method
// but on the super-class
this._super = _super[name];
// The method only need to be bound temporarily, so we
// remove it when we're done executing
var ret = fn.apply(this, arguments);
this._super = tmp;
return ret;
};
})(name, prop[name]) :
prop[name];
}
// The dummy class constructor
function Class() {
// All construction is actually done in the init method
if (!initializing && this.init)
this.init.apply(this, arguments);
}
// Populate our constructed prototype object
Class.prototype = prototype;
// Enforce the constructor to be what we expect
Class.constructor = Class;
// And make this class extendable
Class.extend = arguments.callee;
return Class;
};
Class.prototype.overwrite = function(prop){
// Copy the properties over onto the new prototype
for (var name in prop) {
// Check if we're overwriting an existing function
this[name] = typeof prop[name] == "function" &&
typeof this[name] == "function" && fnTest.test(prop[name]) ?
(function (name, fn, sufn) {
return function () {
var tmp = this._super;
// Add a new ._super() method that is the same method
// but on the super-class
var me = this;
var meArgs = arguments;
this._super = (function(fn, me, args){
return function(){
fn.apply(me, args)
};
})(sufn, this, arguments);
// The method only need to be bound temporarily, so we
// remove it when we're done executing
var ret = fn.apply(me, meArgs);
this._super = tmp;
return ret;
};
})(name, prop[name], this[name]) :
prop[name];
}
};
})();
var Banana = Class.extend({
eat: function(param){
console.log('Banananrama', param);
}
});
var Apple = Banana.extend({
eat: function(doit){
console.log('Apple');
if (doit){
this._super(doit);
}
}
})
var b = new Banana();
var a = new Apple();
b.eat();
console.log('-');
a.eat();
console.log('-');
a.eat(true);
console.log('-');
var bish = angular.copy(a);
bish.overwrite({
eat: function(doit){
console.log('Bitten');
if (doit){
this._super(doit);
}
}
})
var aish = angular.copy(a);
aish.overwrite({
eat: function(doit){
console.log('Swallowed');
if (doit){
this._super(doit);
}
}
})
console.log('--');
console.log('--');
bish.eat(true);
console.log('-');
aish.eat(true);
console.log('--');
console.log('--');
</script>
<script id="jsbin-source-javascript" type="text/javascript">
(function () {
var initializing = false,
fnTest = /xyz/.test(function () {
xyz;
}) ? /\b_super\b/ : /.*/;
// The base Class implementation (does nothing)
this.Class = function () {};
// Create a new Class that inherits from this class
Class.extend = function (prop) {
var _super = this.prototype;
// Instantiate a base class (but only create the instance,
// don't run the init constructor)
initializing = true;
var prototype = new this();
initializing = false;
// Copy the properties over onto the new prototype
for (var name in prop) {
// Check if we're overwriting an existing function
prototype[name] = typeof prop[name] == "function" &&
typeof _super[name] == "function" && fnTest.test(prop[name]) ?
(function (name, fn) {
return function () {
var tmp = this._super;
// Add a new ._super() method that is the same method
// but on the super-class
this._super = _super[name];
// The method only need to be bound temporarily, so we
// remove it when we're done executing
var ret = fn.apply(this, arguments);
this._super = tmp;
return ret;
};
})(name, prop[name]) :
prop[name];
}
// The dummy class constructor
function Class() {
// All construction is actually done in the init method
if (!initializing && this.init)
this.init.apply(this, arguments);
}
// Populate our constructed prototype object
Class.prototype = prototype;
// Enforce the constructor to be what we expect
Class.constructor = Class;
// And make this class extendable
Class.extend = arguments.callee;
return Class;
};
Class.prototype.overwrite = function(prop){
// Copy the properties over onto the new prototype
for (var name in prop) {
// Check if we're overwriting an existing function
this[name] = typeof prop[name] == "function" &&
typeof this[name] == "function" && fnTest.test(prop[name]) ?
(function (name, fn, sufn) {
return function () {
var tmp = this._super;
// Add a new ._super() method that is the same method
// but on the super-class
var me = this;
var meArgs = arguments;
this._super = (function(fn, me, args){
return function(){
fn.apply(me, args)
};
})(sufn, this, arguments);
// The method only need to be bound temporarily, so we
// remove it when we're done executing
var ret = fn.apply(me, meArgs);
this._super = tmp;
return ret;
};
})(name, prop[name], this[name]) :
prop[name];
}
};
})();
var Banana = Class.extend({
eat: function(param){
console.log('Banananrama', param);
}
});
var Apple = Banana.extend({
eat: function(doit){
console.log('Apple');
if (doit){
this._super(doit);
}
}
})
var b = new Banana();
var a = new Apple();
b.eat();
console.log('-');
a.eat();
console.log('-');
a.eat(true);
console.log('-');
var bish = angular.copy(a);
bish.overwrite({
eat: function(doit){
console.log('Bitten');
if (doit){
this._super(doit);
}
}
})
var aish = angular.copy(a);
aish.overwrite({
eat: function(doit){
console.log('Swallowed');
if (doit){
this._super(doit);
}
}
})
console.log('--');
console.log('--');
bish.eat(true);
console.log('-');
aish.eat(true);
console.log('--');
console.log('--');</script></body>
</html>
(function () {
var initializing = false,
fnTest = /xyz/.test(function () {
xyz;
}) ? /\b_super\b/ : /.*/;
// The base Class implementation (does nothing)
this.Class = function () {};
// Create a new Class that inherits from this class
Class.extend = function (prop) {
var _super = this.prototype;
// Instantiate a base class (but only create the instance,
// don't run the init constructor)
initializing = true;
var prototype = new this();
initializing = false;
// Copy the properties over onto the new prototype
for (var name in prop) {
// Check if we're overwriting an existing function
prototype[name] = typeof prop[name] == "function" &&
typeof _super[name] == "function" && fnTest.test(prop[name]) ?
(function (name, fn) {
return function () {
var tmp = this._super;
// Add a new ._super() method that is the same method
// but on the super-class
this._super = _super[name];
// The method only need to be bound temporarily, so we
// remove it when we're done executing
var ret = fn.apply(this, arguments);
this._super = tmp;
return ret;
};
})(name, prop[name]) :
prop[name];
}
// The dummy class constructor
function Class() {
// All construction is actually done in the init method
if (!initializing && this.init)
this.init.apply(this, arguments);
}
// Populate our constructed prototype object
Class.prototype = prototype;
// Enforce the constructor to be what we expect
Class.constructor = Class;
// And make this class extendable
Class.extend = arguments.callee;
return Class;
};
Class.prototype.overwrite = function(prop){
// Copy the properties over onto the new prototype
for (var name in prop) {
// Check if we're overwriting an existing function
this[name] = typeof prop[name] == "function" &&
typeof this[name] == "function" && fnTest.test(prop[name]) ?
(function (name, fn, sufn) {
return function () {
var tmp = this._super;
// Add a new ._super() method that is the same method
// but on the super-class
var me = this;
var meArgs = arguments;
this._super = (function(fn, me, args){
return function(){
fn.apply(me, args)
};
})(sufn, this, arguments);
// The method only need to be bound temporarily, so we
// remove it when we're done executing
var ret = fn.apply(me, meArgs);
this._super = tmp;
return ret;
};
})(name, prop[name], this[name]) :
prop[name];
}
};
})();
var Banana = Class.extend({
eat: function(param){
console.log('Banananrama', param);
}
});
var Apple = Banana.extend({
eat: function(doit){
console.log('Apple');
if (doit){
this._super(doit);
}
}
})
var b = new Banana();
var a = new Apple();
b.eat();
console.log('-');
a.eat();
console.log('-');
a.eat(true);
console.log('-');
var bish = angular.copy(a);
bish.overwrite({
eat: function(doit){
console.log('Bitten');
if (doit){
this._super(doit);
}
}
})
var aish = angular.copy(a);
aish.overwrite({
eat: function(doit){
console.log('Swallowed');
if (doit){
this._super(doit);
}
}
})
console.log('--');
console.log('--');
bish.eat(true);
console.log('-');
aish.eat(true);
console.log('--');
console.log('--');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment