|
|
|
|
|
by ryanto
4594 days ago
|
|
Uniform access principle is nice to have in JavaScript, a dynamic language like you pointed you. One of the nice things is it makes refactoring a bit easier. Imagine you have code that reads: person = {
name: "ryan to",
first: function() {
return this.name.split(/\s/)[0];
},
last: function() {
return this.name.split(/\s/)[1];
}
}
But you realize this could be harmful, so you want to change it to: person = {
name: function() {
return this.first + " " + this.last;
},
first: "ryan",
last: "to"
}
All of a sudden your code breaks because person.name needs to become person.name(), person.first() to person.first, etc. That is somewhat hard to track down/refactor in a large JavaScript code base, or any dynamic language code base for that matter.Uniform access principle is, in my opinion, one of the selling points for implementing your own object model in JS. |
|
You break/change something and then the rest of your code wouldn't compile until you fixed it everywhere (and it would show you where). You wouldn't have to worry whether you forgot something since the compiler wouldn't let you get away with it. Refactoring tools on top of TypeScript (like Resharper and Coderush) will eventually be able to safely do these changes quickly across the whole project.