If you want to do this you can use one of the less-commonly-known features of JavaScript, getters and setters.
function MyObjectClassName (val) {
this.name = val;
}
MyObjectClassName.prototype = {
get name() {
console.log("You just accessed .name!");
return this.name;
}
};
var theObject = new MyObjectClassName("James");
var theName = theObject.name;
This example is meant to be self explanatory. When .name appears to be directly accessed on the last line, it will execute the name() function and print to the console.
Be forewarned that this is considered one of the more dangerous features of JavaScript and should not really be used if you expected people to understand your code or have respect for your attention to security (arbitrary code gets executed when the attribute is accessed).