问题
I know this question title looks scary, but it isn't. Sorry!
Ok, so, what's the point of creating a one-time-only/unchangeable "variable"?
Lets say I have one property called "name" in a Person object.
const Person = {
name: 'Luis Felipe Zaguini'
};
Alright. So, it's incredibly common to see people doing this:
let personName = Person.name;
console.log(`I've written my name, and it is ${personName}.`);
And that's it. In the majority of times that variable is used only once. Or, sometimes, it's referenced in other statements, but you know, it's useless because there IS a way to reference it without setting a new variable.
Tecnically, you're wasting CPU memory, allocating memory for something useless because you can, in fact, type Person.name
multiple times, and do this:
console.log(`I've written my name, and it is ${Person.name}.`);
Also, you're wasting time and adding more lines to your code. Am I overracting? Lot of programmers do this kind of stuff, but personally it doesn't seem to fit very well to me.
回答1:
There are any number of reasons
A 'const` variable will prevent the value from accidentally changing, e.g. if you have written a closure that includes a variable that is accessible outside immediate scope.
Javascript engines are not yet capable of common subexpression elimination, which is a very common compiler optimization. Using a temporary variable could improve performance, like in this example.
Sometimes a variable with a different name can clarify the functionality; see Writing Really Obvious Code (ROC). Example:
var activeUsername = User.name; //It's not just a user's name, it's the active user's name!
Sometimes that extra variable makes it easier to debug/watch variables
Sometimes you want to set a separate breakpoint during the assignment
Am I overracting?
Yes.
回答2:
That sort of approach is most likely intended to cache a value to prevent having to do a lookup. While it generally isn't an issue, using dot notation or indexing an array value does incur a tiny bit of overhead.
The developer may have chosen to do this to clarify part of their code.
Finally, the developer may just sloppy.
In all cases, the possible benefits or side effects all depend on which JavaScript engine you're using and what types of optimizations it does.
回答3:
I guess I got your question. The value is stored in variable instead of using directly is because it prevents multiple roundtrips to the database or it saves time consumed in resolving the expression in case its used multiple times. for eg. in your case, Compiler would prefer to read Person.name from a variable if more than one refrences are there in your code rather than resolving the Person.name each time.
回答4:
I personally don't do this. But as you asked, this is what i think.
const Person = {
name: 'Luis Felipe Zaguini'
};
const personName = Person.name;
personName = personName + " rick"; //As in they want to make some changes to the name but they don't want to change the main object i.e Person
console.log(`I've written my name, and it is ${personName}.`);
I personally prefer to do this. Here, if you want to make changes to the name, you have to change the main object directly. Which is bit risky as you might be using it somewhere else in the code.
const Person = {
name: 'Luis Felipe Zaguini'
};
console.log(`I've written my name, and it is ${Person.name}.`);
回答5:
friend, here you are confusing yourself between an object property and a variable. For eg, name= "foo" has no significance and can be used directly. but Person.name="foo" signifies a Person having name property value as foo.C# is all about objects and memory can always be released in finally code block. Hope I am clear :)
来源:https://stackoverflow.com/questions/42172002/why-do-people-use-variables-in-some-cases