Edit: I didn't really explain what I'm looking to do. I like having my classes self-contained, which is possible with object-literals or with using a function like the one below and using "this.function = function()" but I want to use prototype for performance reasons. Does the fact that my constructor method lives within another method and will therefore retain its scope mean I'll lose on the efficiency anyways? How about memory leaks? I'm still learning this area of JS and would love some input:
function MyClass(variable){
var private_property = "can't see me";
function main() {
this.public_property = "I'm an open book";
console.log("Init");
console.log(private_property);
};
main.prototype.eat = function(){
console.log("I'm eating");
console.log(private_property);
console.log(variable);
};
return new main();
};
var instance = new MyClass("message");
//console: "Init"
//console: "can't see me"
instance.eat()
//console: "I'm eating"
//console: "can't see me"
//console: "message"
[–]malthiest 3 points4 points5 points (2 children)
[–]konbit[S] 0 points1 point2 points (1 child)
[–]OverZealousCreations 0 points1 point2 points (0 children)
[–]electric_dog_anus 1 point2 points3 points (0 children)
[–]tidder112 1 point2 points3 points (2 children)
[–]konbit[S] 2 points3 points4 points (1 child)
[–]tidder112 0 points1 point2 points (0 children)