From your question, I assume you've considered but rejected using composition:
var Player = function (socket, x, y)
{
this.socket = socket;
this.x = x;
this.y = y;
};
Usage:
var player = new Player(socket, x, y);
player.socket.on(/*...*/);
player.socket.emit(/*...*/);
FWIW, I would probably use composition.
But if you don't want to, you have at least a couple of choices:
Really inherit, using socket as the prototype of the new object:
In this scenario, you probably wouldn't use new:
var Player = function (socket, x, y)
{
var obj = Object.create(socket);
obj.x = x;
obj.y = y;
return obj;
};
Usage:
var player = Player (socket, x, y); // No `new` here, although it's harmless
player.on ('event1', function () {});
player.emit ('event2', function () {});
(Although if you did use new, it would be harmless — the object created by new would just be thrown away, because Player returns a non-null object reference, and so overrides the default result of new.)
This won't work if you're relying on the objects inheriting Player.prototype, however. You could copy everything from Player.prototype onto the instance by adding this code to the above:
var key;
for (key in Player.prototype) {
if (Player.prototype[key] !== Object.prototype[key]) {
this[key] = Player.prototype[key];
}
}
That copies any properties from Player.prototype (and its prototypes, except for Object.prototype as the player will already have those) onto the player object. Less than ideal, but functional, it just means that you need to make sure that Player.prototype has everything on it before you create the first player. Also, instanceof won't work for players.
Just wire up the bits you want, without actually using socket as the prototype, by adding bound functions to your Player instance that actually route to socket:
var Player = function (socket, x, y)
{
this.on = socket.on.bind(socket);
this.emit = socket.emit.bind(socket);
this.x = x;
this.y = y;
};
Usage is then as in your question, using new.
Or a bit more verbosely, but giving you the opportunity to get in the middle if you needed:
var Player = function (socket, x, y)
{
this.socket = socket;
this.x = x;
this.y = y;
};
Player.prototype.on = function() {
return this.socket.on.apply(this.socket, arguments);
};
Player.prototype.emit = function() {
return this.socket.emit.apply(this.socket, arguments);
};
Both of the above use features from ES5 that can be polyfilled. From Socket, on, and emit, I'm guessing you're using NodeJS, and so you're using V8 and you have those features. If not, though, and for others with similar questions: The first solution uses the single-argument version of Object.create, the second uses Function#bind. If you need to support older JavaScript engines, just look up polyfills for them, both are quite small.