So, first question here - please be gentle.
I'm working on a fairly JavaScript heavy project with a few other developers from various non-web programming backgrounds and we've decided to try and use public and private methods and properties in our JavaScript psuedo-classes purely as a good coding practice (ie. we know theres no actual advantage or security in it)
We've toyed with a few different ways of doing public & private (ie. using locally scoped variables and functions with privileged methods for public consumption) and we've currently settled on having our JavaScript class constructors actually return an object that represents only their public interface, effectively hiding everything else.
Here's an example:
function MyObject()
{
var _this = this;
this._privateProperty = 'somevalue';
this._privateMethod = function()
{
// Do Something
}
this.public =
{
publicProperty : _this._privateProperty,
publicMethod : function(){ return _this.privateMethod() }
}
return this.public;
}
Which when instantiated and logged in Chrome:
var obj = new MyObject();
console.log(obj);
Outputs:
> Object
> publicMethod: function (){ return _this.privateMethod() }
> publicProperty: "somevalue"
>__proto__: Object
Now to my question:
Because were returning the public interface from the constructor as new object, when you console.log you'll notice that it identifies itself as > Object
- whereas if we don't return that public interface it is identified as > MyObject
.
Ideally, we'd like to have the latter displayed for debugging purposes and I know how to access the "MyObject" name of the contstructor with _this.constructor.name
, but have no idea how to set it so its recognized that way.
Does anyone know how to manually set this?
Note:
I know this is in some ways a bastardization of JavaScript convention and trying to fit a square peg in a round hole, but we found it to be a very obvious and readable way to accomplish what we were trying to do. I'm open to suggestions on how to accomplish this with a different design, but I'm ultimately looking for an answer that fits our current design.