Skip to content

Instantly share code, notes, and snippets.

@tyfkda
Last active August 29, 2015 14:14
Show Gist options
  • Save tyfkda/1cf35af27709d1722df6 to your computer and use it in GitHub Desktop.
Save tyfkda/1cf35af27709d1722df6 to your computer and use it in GitHub Desktop.
Class definition in JavaScript
function defineClass(props) {
var parent = props.parent;
var ctor = props.init || (parent ? function() { parent.apply(this, arguments); }
: function() {});
if (parent)
ctor.prototype = Object.create(parent.prototype);
for (var key in props)
ctor.prototype[key] = props[key];
return ctor;
}
var Foo = (function() {
var Parent = Bar;
var Foo = defineClass({
parent: Super,
init: function(name) {
var self = this;
//Super.call(self, name); // 明示的に親クラスのコンストラクタを呼び出す必要がある
console.log('Foo#init');
},
hello: function() {
var self = this;
console.log('Foo#hello, ' + self.name);
//Super.prototype.hello.call(self); // super呼び出し、ダルい…
},
});
return Foo;
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment