Have you ever wondered if function arguments automatically become private variables within the function? I came across this interesting concept and decided to experiment:
var node = function(nParent,nName,nContent){
this.init = function(){
alert(nName+" "+nParent+" "+nContent);
}
};
var a = new node("A - parent","A - name","A - content");
var b = new node("B - parent","B - name","B - content");
a.init();
b.init();
Surprisingly, the passed in arguments were displayed correctly through alerts. Could this mean that we don't need to explicitly assign them to private variables like in the example below?
var node = function(nParent,nName,nContent){
var parent = nParent;
var name = nName;
var content = nContent;
this.init = function(){
alert(name+" "+parent+" "+content);
}
};
While the second version would be necessary for extra validation checking, it seems like a waste of space if the arguments are already treated as private variables. What do you think, is this a reasonable approach to take? Thanks,