Created
November 11, 2015 21:10
-
-
Save tksmaru/f3e0c9e1f7e935f9c738 to your computer and use it in GitHub Desktop.
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
| module.exports = Authentication; | |
| // @Constructor | |
| function Authentication(name) { | |
| this._name = name; | |
| }; | |
| Authentication.prototype._login = function(loginId, password) { | |
| throw new Error("not implemented"); | |
| }; | |
| Authentication.prototype.login = function(loginId, password) { | |
| this._login(loginId, password).then(function(data) { | |
| console.log("login success."); | |
| console.log(data); | |
| }).catch(function(err) { | |
| console.log("login failure"); | |
| console.log(err); | |
| }); | |
| }; | |
| Authentication.prototype.getName = function() { | |
| return this._name; | |
| }; | |
| Authentication.prototype.echo = function(text) { | |
| console.log(text); | |
| }; |
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
| var IceWall = require("./icewall"); | |
| debugger; | |
| var inst = new IceWall(); | |
| inst.getName(); | |
| inst.echo("hi hi"); | |
| inst.login("myname", "mypassword"); |
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
| var Authentication = require("./authentication"); | |
| var Util = require("./util"); | |
| module.exports = IceWall; | |
| function IceWall() { | |
| Authentication.call(this, "icewall"); | |
| }; | |
| Util.inherits(IceWall, Authentication); | |
| IceWall.prototype._login = function(loginId, password) { | |
| console.log("login to icewall : " + loginId); | |
| return Promise.resolve(loginId); | |
| }; |
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
| exports.inherits = function(childConstructor, parentConstructor) { | |
| Object.setPrototypeOf(childConstructor.prototype, parentConstructor.prototype); | |
| }; |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
ここを参考にした
http://qiita.com/norami_dream/items/ea3827f05699afcb1cc5
http://www.yunabe.jp/docs/javascript_class_in_google.html