Closed park9140 closed 8 years ago
Are we basically talking about C#'s nameof
operator?
Hadn't used it before but seems right.
Is this a dupe of #394 then?
@danquirk, seems like this is different.
@danquirk, seems to me that the initial suggestion on #394 is quite different. This nameof
methodology is primarily aimed at elmination of magic strings that cannot be refactored. Specifically the pluck use case isn't solved by checking the literal argument. The proposal in #394 is dangerous as noted in some of the comments and would either need runtime gating or to prevent all non literal strings from being parameters to a method describe with the memberof
methodology. It also does not handle using Interface names for injection purposes as the nameof
methodology would.
I fully agree with @park9140.
Also, the nameof
methodology can be used when working with Object.observe() method.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/observe
// A user model
var user = {
id: 0,
name: 'Brendan Eich',
title: 'Mr.'
};
// Create a greeting for the user
function updateGreeting() {
user.greeting = 'Hello, ' + user.title + ' ' + user.name + '!';
}
updateGreeting();
Object.observe(user, function(changes) {
changes.forEach(function(change) {
// Any time name or title change, update the greeting
if (change.name === nameof(user.name) || change.name === nameof(user.title)) {
updateGreeting();
}
});
});
looks like another duplicate of #3628
I have been looking at dependency injection and a few issues with refactoring in regards to
_.pluck
. One of the primary things I run into is that I want these things to enforce the interface for refactoring.A few examples
Interface used as a symbol for dependency injection
would compile to
In the case of an interface like pluck we could do something like this
would compile to
In this way we could enforce some level of type safety in common javascript reflection use cases.