`

NodeJs OOP的实现

 
阅读更多

NodeJs OOP的实现

 

   ModuleA.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 + "\n" +
			" public_name is " + this.public_name + "\n" +
			" static_public_name is " + ModuleA.static_public_name + "\n" +
			" static_private_name is" + static_private_name);
	}
}

module.exports = ModuleA;

 

    ModuleB.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();

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics