First: do not name your function Object , it obscures the global constructor of Object .
I see no reason why you need to assign naba inside bar instance. Why are you doing it? You can assign both bar and naba function prototype:
function MyConstructor() { this.foo = 0; } MyConstructor.prototype.bar = function () { }; MyConstructor.prototype.naba = function () {
This ultimately depends on how you call the naba function. The fact that you assign it to this suggests that you want to call it
var obj = new MyConstructor(); obj.naba();
If you want to add naba only after calling bar , you can access foo through this.foo :
MyConstructor.prototype.bar = function () { if(!this.naba) { this.naba = function() {
If you need the correct / best answer, you must show how you are going to use naba .
source share