incubator-flex-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Kevin Newman <Capta...@unFocus.com>
Subject Re: ABC -> JS
Date Wed, 05 Dec 2012 22:39:02 GMT
That's almost exactly the polyfill for Object.create (except with some 
extra book keeping):
https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Object/create

|if| |(!Object.create) {|
|||Object.create = ||function| |(o) {|
|||if| |(arguments.length > 1) {|
|||throw| |new| |Error(||'Object.create implementation only accepts the 
first parameter.'||);|
|||}|
|||function| |F() {}|
|||F.prototype = o;|
|||return| |new| |F();|
|||};|
|}

// classic inheritance works from Object.create without any glue code at all
ChildClass.prototype = Object.create( ParentClass.prototype );
|

You could rewrite the base.js version slightly, to take advantage of 
Object.create (if there's an advantage the native method), and still do 
the bookkeeping:

||
|apache.extend = function( child, parent ) {
    child.$super$ = parent.prototype;
    child.prototype = Object.create( parent.prototype );
    child.prototype.constructor = parent;
}|
|// fallback to base.js method if Object.create polyfill is undesired|
|if| |(!Object.create) {
   apache.extend = ||function( child, parent ) {
     function F() {};
     F.prototype = parent.prototype;
     child.$super$ = parent.prototype;
     child.prototype = new F();
     child.prototype.constructor = child;
   };
}
|

Object.create has other useful features too (that can't be polyfilled 
unfortunately) - it has a way to set properties enumerable, 
configurable, and writable and to define getters and setters. Having a 
mode to output to that might be pretty spiffy.

Kevin N.





Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message