One of the main purposes of the `nameof` operator is to provide the string
value of a symbol, so that if you perform a "Rename" refactoring of that symbol
that the change is also reflected. This is primarily for cases where you
perform precondition assertions tied to an argument:
```
...
static void Method(string x) {
if (x == null) throw new ArgumentNullException(nameof(x));
...
}
```
Now, if I later rename `x`, I don't need to also find any string literals of
"x" and manually update them.
There are other uses of `nameof`, but they all boil down to roughly the same
thing.
Ron
________________________________
From: Isiah Meadows<mailto:[email protected]>
Sent: 8/8/2015 7:23 PM
To: Behrang Saeedzadeh<mailto:[email protected]>; EcmaScript Discuss Mailing
List<mailto:[email protected]>
Subject: Re: What do you think about a C# 6 like nameof() expression for
JavaScript.
Call me crazy, but I don't see anything that couldn't be done more concisely
with a string literal. Is it supposed to be able to do this?
```js
function foo(x) {
return nameof(x);
}
foo(bar); // "bar";
```
In that case, the engine would have to keep track of usages as well, in a
similar sense as `arguments.callee`, and if it were a function, it would make
optimization quite difficult, as engines don't have the capacity to statically
analyze that such a function is used.
If it is like `typeof`, we now have a breaking change - a keyword that was a
valid Identifier before.
```js
// Error?
function nameof(value) {
return
value.name<https://na01.safelinks.protection.outlook.com/?url=http%3a%2f%2fvalue.name&data=01%7c01%7cron.buckton%40microsoft.com%7ca2e2c4d35400435810d008d2a061897d%7c72f988bf86f141af91ab2d7cd011db47%7c1&sdata=pwV45avF9RX6COETpoLIY4EF%2bmCVmk6kEEmLc2JXSCY%3d>;
}
var bar = {name: 2};
nameof(bar); // "bar" or 2?
```
I don't think this is going to work out in practice, not in ECMAScript proper.
You might appreciate Sweet.js, though.
On Sat, Aug 8, 2015, 21:27 Behrang Saeedzadeh
<[email protected]<mailto:[email protected]>> wrote:
Forgot to mention that nameof works with local variables too:
function foo() {
var aNum = 1;
console.log(nameof(aNmum), aNum);
}
On Sat, Aug 8, 2015 at 10:38 AM Behrang Saeedzadeh
<[email protected]<mailto:[email protected]>> wrote:
So basically we could use it like this:
function aFunc(aParam) {
throw new Error(nameof(aParam));
}
and nameof(aParam) would return the string "aParam".
This is possible to do even right now using arguments.callee and some hacky
code, but having it built-in to spec would be nicer IMHO.
--
Best regards,
Behrang Saeedzadeh
--
Best regards,
Behrang Saeedzadeh
_______________________________________________
es-discuss mailing list
[email protected]<mailto:[email protected]>
https://mail.mozilla.org/listinfo/es-discuss<https://na01.safelinks.protection.outlook.com/?url=https%3a%2f%2fmail.mozilla.org%2flistinfo%2fes-discuss&data=01%7c01%7cron.buckton%40microsoft.com%7ca2e2c4d35400435810d008d2a061897d%7c72f988bf86f141af91ab2d7cd011db47%7c1&sdata=7DHMx5gTd2OexSlKscSrKlMIxABMUkOKRC%2fuCbc6pWk%3d>
_______________________________________________
es-discuss mailing list
[email protected]
https://mail.mozilla.org/listinfo/es-discuss