Skip to content

Instantly share code, notes, and snippets.

@sebmarkbage
Created January 21, 2011 21:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sebmarkbage/790455 to your computer and use it in GitHub Desktop.
Save sebmarkbage/790455 to your computer and use it in GitHub Desktop.
// Util
function placeholder(){}
function derive(Child, Parent){
placeholder.prototype = Parent.prototype
var proto = new placeholder()
for (var i = 2, l = arguments.length; i < l; i++){
var mixin = arguments[i]
for (var key in mixin) proto[key] = mixin[key]
}
return Child.prototype = proto
}
// Element
function Element(){
}
Element.prototype = {
render: function(){
}
}
// Transform
function Transform(){
}
Transform.prototype = {
rotate: function(){
}
}
// Shape
function Shape(){
this._()
}
Shape.prototype = derive(Element, Transform, {
_: Element,
_render: Element.prototype.render,
render: function(){
this._render()
},
draw: function(){
}
})
// Triangle
function Triangle(){
this.__()
}
Triangle.prototype = derive(Shape, {
__: Shape,
__draw: Shape.prototype.draw,
__render: Shape.prototype.render,
draw: function(){
this.__draw()
},
render: function(){
this.__render()
}
})
@subtleGradient
Copy link

um, ewww?

@subtleGradient
Copy link

I think I'd rather see something more explicit and specific.
Or at the very least a code comment explaining what the underscores are actually for.

@sebmarkbage
Copy link
Author

Updated to something more explicit.

@steida
Copy link

steida commented Jan 22, 2011

I haven't any special prototyping style, because pure JS is actually real codding style in Google Closure ;) Pure JS with goog.inherits function is fine enough. http://google-styleguide.googlecode.com/svn/trunk/javascriptguide.xml

@steida
Copy link

steida commented Jan 22, 2011

In the other words, you eunuchized live prototype inheritance for sake of composed objects. If someone override base method later, children __________ references will never be updated.

@steida
Copy link

steida commented Jan 25, 2011

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment