如何删除变量? [英] How to delete a variable?

查看:230
本文介绍了如何删除变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的javascript代码,我想删除变量代码,使其值未定义。

  var code = $(this).data('code'); 
var userelm = $(this);

这里我正在检查:



< pre class =lang-js prettyprint-override> if($('。code-1')。val()!=''&& $('。code-2') .val()!=''&& $('。code-3')。val()!=''){
if(code == $('。code-1')。 val()+ $('。code-2')。val()+ $('。code-3')。val()){
$('。overlay')。remove();
$('。code-box')。remove();
$('。close-lock')。remove();
userelm.ajaxloader(); //自己的函数
userelm.off();
删除代码;
console.log(代码);
删除userelm;
}
}

为什么这个程序不会删除代码变量所以它的值是undefined?

解决方案

在JavaScript中删除变量:



摘要:



您在JavaScript中删除变量时遇到问题的原因是:因为JavaScript不会让你。您不能删除由 var 命令创建的任何内容,除非我们将一只兔子拉出我们的技巧。



delete命令仅适用于未使用var创建的对象属性。



JavaScript允许您在以下条件下删除使用var创建的变量:


  1. 您正在使用javascript解释器或命令行。


  2. 您正在使用eval并创建和删除var在那里。


在终端上演示,使用删除boo 删除(boo)命令。使用 js 命令行终端的演示让你删除变量。

  el @ defiant~ $ js 

js> typeof boo
undefined

js> boo
typein:2:ReferenceError:boo未定义

js> boo = 5
5

js> typeof boo
number

js>删除(boo)
true

js> typeof boo
undefined

js> boo
typein:7:ReferenceError:boo未定义

如果你必须在JavaScript中将变量设置为undefined,您有一个选项:



javascript页面中的演示:将其放入 myjs.html

 < html> 
< body>
< script type =text / JavaScript>
document.write(aliens:+ aliens +< br>);
document.write(typeof aliens:+(typeof aliens)+< br>);
var aliens =争夺尼米兹;
document.write(发现一些外星人:+(外星人类型)+< br>);
document.write(不是说外星人但是......+外星人+< br>);
aliens = undefined;
document.write(外星人设置为未定义< br>);
document.write(typeof aliens:+(typeof aliens)+< br>);
document.write(你确定他们走了?+外星人);
< / script>
< / body>
< / html>

使用浏览器打开myjs.html,打印出来:

 外星人:undefined 
typeof外星人:undefined
找到一些外星人:字符串
不是说外星人但是......争夺nimitz
外星人设置为未定义
typeof外星人:undefined
你确定他们已经离开? undefined

警告当您将变量设置为时undefined 您正在为另一个变量分配一个变量。如果有人通过运行 undefined ='gotcha!'来中毒,那么无论何时将变量设置为undefined,它都会变为:gotcha!。



我们应该如何检查变量是否没有值?



使用null而不是undefined如下:

  document.write(skittles:+ skittles +< br>); 
document.write(typeof skittles:+(typeof skittles)+< br>);
var skittles = 5;
document.write(skittles:+ skittles +< br>);
document.write(typeof skittles:+ typeof skittles +< br>);
skittles = null;
document.write(skittles:+ skittles +< br>);
document.write(typeof skittles:+ typeof skittles);

打印:

 吃喝玩乐:undefined 
类型的吃喝玩乐:undefined
吃喝玩乐:5
类型的吃喝玩乐:数字
吃喝玩乐:null
typeof skittles:object

如果你没有使用strict,你可以删除这样创建的变量:

 < script type =text / JavaScript> 
//使用严格
a = 5;
document.writeln(typeof a); //打印号码
删除a;
document.writeln(typeof a); //打印undefined
< / script>

但如果您取消注释使用严格, javascript将无法运行。


Here is my javascript code, I want to delete the variable code so that it has value undefined.

var code = $(this).data('code');
var userelm = $(this);

Here I'm doing the check:

if($('.code-1').val()!='' && $('.code-2').val()!='' && $('.code-3').val()!=''){
    if(code==$('.code-1').val()+$('.code-2').val()+$('.code-3').val()){
        $('.overlay').remove();
        $('.code-box').remove();
        $('.close-lock').remove();
        userelm.ajaxloader(); //own function
        userelm.off();
        delete code;
        console.log(code);
        delete userelm;
    }
}

Why does this program not remove the code variable so it has the value undefined?

解决方案

Delete a variable in JavaScript:

Summary:

The reason you are having trouble deleting your variable in JavaScript is because JavaScript won't let you. You can't delete anything created by the var command unless we pull a rabbit out our bag of tricks.

The delete command is only for object's properties which were not created with var.

JavaScript will let you delete a variable created with var under the following conditions:

  1. You are using a javascript interpreter or commandline.

  2. You are using eval and you create and delete your var inside there.

Demo on terminal, Use the delete boo or delete(boo) command. A demo with js command line terminal let you delete a variable.

el@defiant ~ $ js

js> typeof boo
"undefined"

js> boo
typein:2: ReferenceError: boo is not defined

js> boo=5
5

js> typeof boo
"number"

js> delete(boo)
true

js> typeof boo
"undefined"

js> boo
typein:7: ReferenceError: boo is not defined

If you MUST set your variable to undefined in JavaScript, you have one option:

Demo in javascript page: put this in myjs.html:

<html>
<body>
    <script type="text/JavaScript">
        document.write("aliens: " + aliens + "<br>");
        document.write("typeof aliens: " + (typeof aliens) + "<br>");
        var aliens = "scramble the nimitz";
        document.write("found some aliens: " + (typeof aliens) + "<br>");
        document.write("not sayings its aliens but... " + aliens + "<br>");
        aliens = undefined;
        document.write("aliens set to undefined<br>");
        document.write("typeof aliens: " + (typeof aliens) + "<br>");
        document.write("you sure they are gone? " + aliens);
    </script>
</body>
</html>

Open myjs.html with a browser, it prints this:

aliens: undefined
typeof aliens: undefined
found some aliens: string
not sayings its aliens but... scramble the nimitz
aliens set to undefined
typeof aliens: undefined
you sure they are gone? undefined

Warning When you set your variable to undefined you are assigning a variable to another variable. If someone poisons the well by running undefined = 'gotcha!', then whenever you set your variable to undefined, it becomes: "gotcha!".

How should we check if a variable has no value?

Use null instead of undefined like this:

document.write("skittles: " + skittles + "<br>");
document.write("typeof skittles: " + (typeof skittles) + "<br>");
var skittles = 5;
document.write("skittles: " + skittles + "<br>");
document.write("typeof skittles:" + typeof skittles + "<br>");
skittles = null;
document.write("skittles: " + skittles + "<br>");
document.write("typeof skittles: " + typeof skittles);

Which prints:

skittles: undefined
typeof skittles: undefined
skittles: 5
typeof skittles:number
skittles: null
typeof skittles: object 

If you are not using strict, you can delete variables created like this:

<script type="text/JavaScript">
   //use strict
   a = 5;
   document.writeln(typeof a);        //prints number
   delete a;
   document.writeln(typeof a);        //prints undefined
</script>

but if you uncomment use strict, the javascript will not run.

这篇关于如何删除变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆