NodeJs OOPの実現

2001 ワード

もっと読む
NodeJs OOPの実現
 
   Module A.js
 
//static private
var static_private_name = "ModuleA_static_private_name";
//static public
ModuleA.static_public_name = "ModuleA_static_public_name";

function ModuleA() {
	this.public_name = "ModuleA_public_name";
	var private_name = "ModuleA_private_name";

	this.public_hello = function() {
		console.log("hello, my private_name is " + private_name + "
" + " public_name is " + this.public_name + "
" + " static_public_name is " + ModuleA.static_public_name + "
" + " static_private_name is" + static_private_name); } } module.exports = ModuleA;
 
    Module B.js
 
   
//static private
var static_private_name = "ModuleB_static_private_name";
//static public
ModuleB.static_public_name = "ModuleB_static_public_name";

//extends class
var superCls = require('./ModuleA');
require('util').inherits(ModuleB, superCls);
//constuctor
function ModuleB() {
	//super();
	superCls.call(this);

	//public prop
	this.public_name = "ModuleB_public_name";
	
	var private_name = "ModuleB_private_name";
        //override super method
	var superPublic_hello = this.public_hello;
	this.public_hello = function() {
		console.log("override");
		superPublic_hello.call(this);
	}
}

module.exports = ModuleB;
 
  
var ModuleA = require("./ModuleA");
var ModuleB = require("./ModuleB");

var insA = new ModuleA();
//insA.public_hello();

var insB = new ModuleB();
insB.public_hello();