最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to reset a variables value (delete - undefined - null) - Stack Overflow

programmeradmin1浏览0评论

I read if you declare a variable you can not delete it.
If you leave it undeclared you can.
I also read if you do not declare a variable it can cause issues later.

Certain lints I have used have pointed out declared and undeclared issues in some of the stuff I have written, to stop the reporting of these issues and based off of all the information I have read thus far, I have opted to declare all my variables.

Now here is where this bees confusing for me.

I am trying to build a random name generator that utilizes a Math.random routine, when I try to recycle this routine it adds to the previous one, I have tried to set the var undefined / null (which in themselves can be variables that are defined) and it still just adds the new results to the old ones, this is making the feature of regenerating a new name if you do not like the old one useless.

After writing this I realized my question is two part:

  • How to do you delete a value from a var and restart?
  • Is declaring your variables really that important?

I have yet to find any solid information on the difference between declared and undeclared other than it changes the inherent properties of the variables (mainly if a variable can be deleted or not)

var nameLength = Math.floor(Math.random() * (13 - 3) + 3); /* min 3 max 13*/
testValue = Math.floor(Math.random() * (13 - 3) + 3);
var pareLetter = 0;
var randomLetter = "";
var randomName = "";
var capitolLetter = "";
var checkLetter = 0;
var nameTitle = "XXX"; /* titles add _ between title and name*/
var nameLetters = [
    ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"],
    ["a", "e", "i", "o", "u"],
    ["b", "c", "d", "f", "g", "h", "j", "k", "l", "m", "n", "p", "q", "r", "s", "t", "v", "w", "x", "y", "z"]
];
if (nameTitle !== "") { /* my attempt to subtract title length +1 from name length if over 13*/
    nameLength = nameLength + (nameTitle.length + 1);
    if (nameLength > 13) {
        nameLength = nameLength - (nameLength - 13);
    }
}
for (i = 0; i < nameLength; i++) {
    randomLetter = nameLetters[pareLetter][Math.floor(Math.random() * nameLetters[pareLetter].length)];
    checkLetter = nameLetters[1].indexOf(randomLetter);
    if (checkLetter > -1) { /* my version of keeping consanants and vowels from stacking up */
        pareLetter = 2;
    } else {
        pareLetter = 1;
    }
    if (randomName.length < 1) { /* my version to keep first letter capitolized*/
        capitolLetter = randomLetter.toUpperCase();
        randomName = randomName + capitolLetter;
    } else {
        randomName = randomName + randomLetter;
    }
}
if (nameTitle !== "") {
    alert(nameTitle + "_" + randomName);
} else {
    alert(randomName);
}
alert("this is nameLength" + nameLength);
alert("this is testValue " + testValue);
delete nameLength;
delete testValue;
alert("this is nameLength" + nameLength);
alert("this is testValue " + testValue); /* notice this was not displayed*/
/

here is what I am working with, keep in mind that fiddle seems to GC after each run, the environment I am setting this up in does not do that.

I read if you declare a variable you can not delete it.
If you leave it undeclared you can.
I also read if you do not declare a variable it can cause issues later.

Certain lints I have used have pointed out declared and undeclared issues in some of the stuff I have written, to stop the reporting of these issues and based off of all the information I have read thus far, I have opted to declare all my variables.

Now here is where this bees confusing for me.

I am trying to build a random name generator that utilizes a Math.random routine, when I try to recycle this routine it adds to the previous one, I have tried to set the var undefined / null (which in themselves can be variables that are defined) and it still just adds the new results to the old ones, this is making the feature of regenerating a new name if you do not like the old one useless.

After writing this I realized my question is two part:

  • How to do you delete a value from a var and restart?
  • Is declaring your variables really that important?

I have yet to find any solid information on the difference between declared and undeclared other than it changes the inherent properties of the variables (mainly if a variable can be deleted or not)

var nameLength = Math.floor(Math.random() * (13 - 3) + 3); /* min 3 max 13*/
testValue = Math.floor(Math.random() * (13 - 3) + 3);
var pareLetter = 0;
var randomLetter = "";
var randomName = "";
var capitolLetter = "";
var checkLetter = 0;
var nameTitle = "XXX"; /* titles add _ between title and name*/
var nameLetters = [
    ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"],
    ["a", "e", "i", "o", "u"],
    ["b", "c", "d", "f", "g", "h", "j", "k", "l", "m", "n", "p", "q", "r", "s", "t", "v", "w", "x", "y", "z"]
];
if (nameTitle !== "") { /* my attempt to subtract title length +1 from name length if over 13*/
    nameLength = nameLength + (nameTitle.length + 1);
    if (nameLength > 13) {
        nameLength = nameLength - (nameLength - 13);
    }
}
for (i = 0; i < nameLength; i++) {
    randomLetter = nameLetters[pareLetter][Math.floor(Math.random() * nameLetters[pareLetter].length)];
    checkLetter = nameLetters[1].indexOf(randomLetter);
    if (checkLetter > -1) { /* my version of keeping consanants and vowels from stacking up */
        pareLetter = 2;
    } else {
        pareLetter = 1;
    }
    if (randomName.length < 1) { /* my version to keep first letter capitolized*/
        capitolLetter = randomLetter.toUpperCase();
        randomName = randomName + capitolLetter;
    } else {
        randomName = randomName + randomLetter;
    }
}
if (nameTitle !== "") {
    alert(nameTitle + "_" + randomName);
} else {
    alert(randomName);
}
alert("this is nameLength" + nameLength);
alert("this is testValue " + testValue);
delete nameLength;
delete testValue;
alert("this is nameLength" + nameLength);
alert("this is testValue " + testValue); /* notice this was not displayed*/
http://jsfiddle/OldGuyGamer/PJsZ3/4/

here is what I am working with, keep in mind that fiddle seems to GC after each run, the environment I am setting this up in does not do that.

Share Improve this question edited Aug 29, 2015 at 10:46 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked Jun 16, 2014 at 12:19 user3742773user3742773 111 gold badge1 silver badge3 bronze badges 5
  • 1 It may be useful to show us a live example of your code, because right now your question is very vague and confusing. You should always declare your variables... – Niet the Dark Absol Commented Jun 16, 2014 at 12:26
  • What exactly do you mean by delete and what do you mean by restart? – Andy Commented Jun 16, 2014 at 12:27
  • 1 ok, let me get my fiddle copied over – user3742773 Commented Jun 16, 2014 at 12:29
  • @user3742773 you can use something like this in your question: jsfiddle/T45g6 – Sergio Commented Jun 16, 2014 at 12:33
  • @user3742773 Click the button here. Notice how nameLength is not defined outside the scope of the onLoad function that JSFiddle wraps your code in? – Niet the Dark Absol Commented Jun 16, 2014 at 12:41
Add a ment  | 

2 Answers 2

Reset to default 2

I read if you declare a variable you can not delete it.

yes. e.g. var foo = 1; delete foo; console.log(foo); // 1

BUT the thing to take a way is .. do not delete a variable

Only delete members. e.g. delete foo.bar

Instead of deleting a variable you can reinitialize it to an empty state i.e. foo = undefined

How to do you delete a value from a var and restart?

You can't delete declared global variables because ECMA-262 says they must be intialised as not deletable. You can't delete declared function variables because you can't access the "variable object" that they are attached to.

Is declaring your variables really that important

Declaring variables constrains their scope to the current execution context. Not declaring a variable means that it bees a property of the global object when the code assigning it a value is executed. If all variables are global, you start running into problems with name clashes, e.g. every counter in a for loop must be different in every loop in every function.

So if you want "deletable variables", then make them properties of some global object and you can treat them more or less like variables and delete them, e.g.

var u = void 0;
var globalObj = {varOne:u, varTwo:u, varThree:u};

delete globalObj.varOne;

You could do something similar using the global object like:

var globalObj = this;
varOne = varTwo = varThree = void 0;

delete globalObj.varOne;

I think the first approach is preferred, however you haven't provided enough detail to provide any objective analysis of which will suit your case better.

Edit

I have yet to find any solid information on the difference between declared and undeclared other than it changes the inherent properties of the variables (mainly if a variable can be deleted or not)

Whether variables are declared or not also changes when the variable is initialised: with var, variables are created and initialised to undefined before any code is executed. Without var, variables don't exist until the code assigning to them is executed (which might be never if assignment is conditional).

发布评论

评论列表(0)

  1. 暂无评论