Created
November 5, 2019 09:14
-
-
Save kates/e16abffe870cf410c24ee636e55c407b to your computer and use it in GitHub Desktop.
A tiny JS framework.
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 Klass = (function() { | |
var Klass = function() {}; | |
Klass.extend = function(constructor, props) { | |
var _super = this.prototype; | |
if (arguments.length < 2) { | |
props = constructor; | |
constructor = function(){}; | |
} | |
if (arguments.length < 1) { | |
props = {}; | |
} | |
var proto = new this(); | |
var classMethods = {}; | |
for(var _name in props) { | |
if (/^\#/.test(_name)) { | |
var name = _name.replace(/^\#/, ""); | |
classMethods[name] = props[_name]; | |
} else { | |
if (/^\./.test(_name)) { | |
var name = _name.replace(/^\./, ""); | |
if (typeof props[_name] == "function") { | |
proto[name] = (function(name, fn) { | |
return function() { | |
var tmp = this._super; | |
this._super = _super[name]; | |
var ret = fn.apply(this, arguments); | |
this._super = tmp; | |
return ret; | |
}; | |
})(name, props[_name]); | |
} | |
} else { | |
proto[name] = props[_name]; | |
} | |
} | |
} | |
var Klass = function() { | |
this.self = Klass; | |
this.self.classMethods = classMethods; | |
var args = Array.apply(null, arguments); | |
args.unshift(this.self); | |
constructor.apply(this, args); | |
}; | |
Klass.prototype = proto; | |
Klass.extend = arguments.callee; | |
if (proto.self) { | |
for (var name in proto.self.classMethods) { | |
if (typeof proto.self.classMethods[name] == "function") { | |
var newFunc = function() { | |
var args = Array.apply(null, arguments); | |
args.unshift(Klass); | |
proto.self.classMethods[name].apply(Klass, args); | |
}; | |
Object.defineProperty(Klass, name, { value: newFunc, writable: true }); | |
} else { | |
Object.defineProperty(Klass, name, { value: proto.self[name], writable: true}); | |
} | |
} | |
} | |
for (var name in classMethods) { | |
if (typeof classMethods[name] == "function") { | |
var newFunc = function() { | |
var args = Array.apply(null, arguments); | |
args.unshift(Klass); | |
classMethods[name].apply(Klass, args); | |
}; | |
Object.defineProperty(Klass, name, { value: newFunc, writable: true }); | |
} else { | |
Object.defineProperty(Klass, name, { value: classMethods[name], writable: true}); | |
} | |
} | |
return Klass; | |
} | |
return Klass; | |
})(); | |
var Tracker = Klass.extend(function(self, serial) { | |
console.log("in constructor", arguments); | |
console.log("in constructor still", self.shit); | |
self.serialNo = serial; | |
},{ | |
"#y": "123123", | |
"#shit": "x", | |
"#configure": function(self, data) { | |
console.log("configuring with " + data); | |
self.shit = data; | |
}, | |
".trackFlightsDeeplink": function(data) { | |
console.log("shit is " + this.self.shit); | |
console.log("Flights Data ", data); | |
console.log("Flights serial ", this.self.serialNo); | |
console.log("Flights y", this.self.y); | |
}, | |
}); | |
Tracker.configure("xxx"); | |
var tracker = new Tracker('123'); | |
Tracker.y = "yyyy"; | |
tracker.trackFlightsDeeplink("aaa"); | |
var Genzo = Tracker.extend({ | |
"#shit": "ooo", | |
".trackHotelsDeeplink": function(data) { | |
console.log("hotel shit is " + this.self.shit); | |
console.log("Hotels Data ", data); | |
console.log("hotels yyy", this.self.y); | |
} | |
}); | |
var Tealium = Tracker.extend({ | |
".tracksFlightsDeeplink": function(data) { | |
console.log("in Tealium", data); | |
} | |
}); | |
var genzo = new Genzo(); | |
genzo.trackFlightsDeeplink("bbb"); | |
genzo.trackHotelsDeeplink("ccc"); | |
var tealium = new Tealium(); | |
tealium.trackFlightsDeeplink("ddd"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment