It is not possible to directly pass a variable to the function inc(obj.prop), because in javascript variables are passed by value, but you can pass the object itself and the name of the variable you want to increment.
Object.prototype.inc = function(n){ this[n]? this[n]++ : this[n] = 1; }
let obj = {};
obj.inc("prop");
// obj.prop == 1
You can also add the required fields to the object before
Object.prototype.has_fields = function(obj2){
for(let p of Object.keys(obj2)){
if(obj2[p].constructor == Object){
if(!this[p]) this[p] = {};
this[p].has_fields(obj2[p]);
}else{
if(!this[p]) this[p] = obj2[p];
}
}
return this;
}
let obj = {};
obj.has_fields({a:{b:{c:{d:{prop:0}}}}});
obj.a.b.c.d.prop++;
// obj.a.b.c.d.prop == 1
obj.has_fields({a:{b:{c:{d:{prop:0}}}}});
obj.a.b.c.d.prop++;
// obj.a.b.c.d.prop == 2