Last active
August 29, 2015 14:14
-
-
Save tyfkda/1cf35af27709d1722df6 to your computer and use it in GitHub Desktop.
Class definition in JavaScript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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