jquery - Is John Resig's OO JavaScript implementation production safe? -
for long time have been throwing around idea of making javascript more object oriented. have looked @ few different implementations of cannot decide if necessary or not.
what trying answer following questions
- is john resig's simple inheritance structure safe use production?
 - is there way able tell how has been tested?
 - besides joose other choices have purpose? need 1 easy use, fast, , robust. needs compatible jquery.
 
huh. looks more complicated needs be, me.
actually looking more closely take exception doing providing this._super() whilst in method, call superclass method.
the code introduces reliance on typeof==='function' (unreliable objects), function#tostring (argh, function decomposition unreliable), , deciding whether wrap based on whether you've used sequence of bytes _super in function body (even if you've used in string. , if try eg. this['_'+'super'] it'll fail).
and if you're storing properties on function objects (eg myclass.myfunction.some_private_constant, might keep namespaces clean) wrapping stop getting @ properties. , if exception thrown in method , caught in method of same object, _super end pointing @ wrong thing.
all make calling superclass's method-of-the-same name easier. don't think that's hard in js anyway. it's clever own good, , in process making whole less reliable. (oh, , arguments.callee isn't valid in strict mode, though that's not fault since occurred after posted it.)
here's i'm using classes @ moment. don't claim “best” js class system, because there loads of different ways of doing , bunch of different features might want add or not add. it's lightweight , aims @ being ‘javascriptic’, if that's word. (it isn't.)
function.prototype.makesubclass= function() {     function class() {         if (!(this instanceof class))             throw 'constructor function requires new operator';         if ('_init' in this)             this._init.apply(this, arguments);     }     if (this!==object) {         function.prototype.makesubclass.nonconstructor.prototype= this.prototype;         class.prototype= new function.prototype.makesubclass.nonconstructor();     }     return class; }; function.prototype.makesubclass.nonconstructor= function() {};   it provides:
protection against accidental missing
new. alternative silently redirectx()new x()missingnewworks. it's toss-up best; went explicit error 1 doesn't used writing withoutnew, causing problems on other objects not defined that. either way better unacceptable js default of lettingthis.properties fall ontowindow, mysteriously going wrong later.an inheritable
_initmethod, don't have write constructor-function nothing call superclass constructor function.
and that's all.
here's how might use implement resig's example:
var person= object.makesubclass(); person.prototype._init= function(isdancing) {     this.dancing= isdancing; }; person.prototype.dance= function() {     return this.dancing; };  var ninja = person.makesubclass(); ninja.prototype._init= function() {     person.prototype._init.call(this, false); }; ninja.prototype.swingsword= function() {     return true; };  var p= new person(true); p.dance(); // => true  var n = new ninja(); n.dance(); // => false n.swingsword(); // => true  // should true p instanceof person && n instanceof ninja && n instanceof person   superclass-calling done naming method want , calling it, bit in python. could add _super member constructor function if wanted avoid naming person again (so you'd ninja._super.prototype._init.call, or perhaps ninja._base._init.call).
Comments
Post a Comment