If I declare a class like this:
var Foo = function() {}; And add properties to it like this:
Foo.prototype.bar = ""; Why can't I access the property like this:
Foo.prototype.setBar = function( value ) { this.bar = value; } In my code I don't have Foo.prototype.bar in scope of Foo.prototype.SetBar. this.bar is showing undefined.
UPDATE
Ok maybe I should be a little more specific since for some reason my code is not running.
var JSocketServer = function( options, callback ) { if( typeof(options) != "object" ) { callback("Invalid object passed for options"); return; } if( typeof(options.port) != "number" ) { callback("Must specify a port number in options"); return; } // Hook up Event Emitter Functionality mevents.EventEmitter.call(this); this.initServer( options, callback ); }; // Set up static class properties JSocketServer.prototype.socketPool = {}; JSocketServer.prototype.socketMap = {}; // Inherit from EventEmitter mutil.inherits(JSocketServer, mevents.EventEmitter); JSocketServer.prototype.initServer = function( options, callback ) { // Hook up raw tcp server var jserver = this; var server = mnet.createServer( function( socket ) { jserver.handleSocket( socket, callback ); }); server.listen(options.port, function() { console.log("Socket Server is bound"); }) this.serverListener( server ) } JSocketServer.prototype.handleSocket = function( socket, callback ) { var jsocketServer = this; var jsocket = new mjsocket(socket); console.log("Socket: "+jsocket.socketID+" connected"); this.socketPool[jsocket.socketID] = jsocket; jsocket.on("data", function( data ) { // Add socket id to socket map jsocketServer.socketMap[jsocket.moduleID] = jsocket.socketID; }); jsocket.on("close", function(err) { jsocketServer.removeSocket(jsocket.socketID); }); // Callback with JSocket callback( undefined, jsocket ); } Now inside JSocketServer.prototype.handleSocket where I'm trying to assign a key and value to this.socketPool, it's saying that this.socketPool is undefined. Now from my understanding and what you guys are saying, this shouldn't be.
UPDATE
Here's a JS Fiddle of my code http://jsfiddle.net/bZrtn/. I have 2 classes JSocketServer and JSocket and they're being used by the APP at the bottom.

barandsetBarare shared properties between instances (since you set them on theprototype)...this.baris different thanFoo.prototype.barvar foo = new Foo(); f.setBar("asdf");,...foo.barequals"asdf", andFoo.prototype.baris still""