Naming var after prop'ty pt II: naming key after method


#1

Continuing the discussion from Is naming a var after a property bad practice?:


In 'This' Works for Everyone, we have an example object with a method created with the new Object(); constructor and are instructed to add a second object and add a method to it... In this case I wanted to write it in object literal notation.

However, following the instructions in this way does result in a line that looks like setAge: setAge...

// here we define our method using "this", before we even introduce bob
var setAge = function (newAge) {
  this.age = newAge;
};
// now we make bob
var bob = new Object();
bob.age = 30;
bob.setAge = setAge;
  
// make susan here, and first give her an age of 25

var susan = {
  age: 25,
  setAge: setAge
};

// here, update Susan's age to 35 using the method

susan.setAge(35);

If I understand correctly, the two variables don't overlap because the key, which allows "illegal" names, is somehow tied to the object (dot notation?) (although I don't think this affects its scope?), while its value refers to the global setAge function variable is a copy of the global setAge function.

I don't think there's any practical difference between Bob and Susan's setAge properties, although it is a lot easier to differentiate between setAge and setAge in dot notation!

As I become more experienced, is this another case in which differentiating between object.methodName and methodName will theoretically become clearer? I did see this post which seemed like a parallel alternative to naming vars after properties, which made me wonder if this were a similar scenario.

Thanks all,
frisby


Is naming a var after a property bad practice?
#2

@msfrisby

should be
a =copy= is made of the setAge =function=


#3

Thank you, @leonhard.wettengmx.n, I'll change it accordingly. To confirm, does that mean that the setAge property contains a second instance of the function? Or is it a reference to the function?