Skip to content

Instantly share code, notes, and snippets.

@sebmarkbage
Created January 21, 2011 21:25
Show Gist options
  • Select an option

  • Save sebmarkbage/790455 to your computer and use it in GitHub Desktop.

Select an option

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
Copy Markdown

um, ewww?

@subtleGradient
Copy link
Copy Markdown

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
Copy Markdown
Author

Updated to something more explicit.

@steida
Copy link
Copy Markdown

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
Copy Markdown

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
Copy Markdown

steida commented Jan 25, 2011

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