如果我在Javascript验证中多次声明相同的变量? [英] if I declare the same variable multiple times in Javascript Validation?

查看:70
本文介绍了如果我在Javascript验证中多次声明相同的变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

var ddlItemtype = document.getElementById('<%=ddlrecdto.ClientID%>');
    var ddlitem = ddlItemtype.options[ddlItemtype.selectedIndex].value;
    if (ddlitem == 0) {
        alert("Please select received to.");
        return false;
    }


    var ddlItemtype = document.getElementById('<%=ddlissuedto.ClientID%>');
    var ddlitem = ddlItemtype.options[ddlItemtype.selectedIndex].value;
    if (ddlitem == 0) {
        alert("Please select issued to.");
        return false;
    }





我的coading有问题吗??



Is there any problem for my coading...?

推荐答案

应该没有任何问题。



但是当你可以重用时,为什么要一次又一次地重写相同的代码变量。

There should not be any problem.

But why to re-write the same code again and again, when you can reuse the variable.
var ddlItemtype = document.getElementById('<%=ddlrecdto.ClientID%>');
var ddlitem = ddlItemtype.options[ddlItemtype.selectedIndex].value;



申报时这些在一个函数中,你可以在函数内的任何地方使用它。



但是,如果你试图访问不同的函数,那么它将无法正常工作仅 Local 变量到该函数。在这种情况下,您需要将函数外部的变量声明为 Global 变量。


When you declare these in a function, you can use it anywhere inside the function.

However, if you will try to access in different function, then it won't work as they are Local variables to that function only. In that case, you need to declare the variables outside the function as Global variables.


只需删除 var ;它只需要解决外部和内部上下文中给定相同名称的变量之间的歧义:在内部上下文中,外部变量是可访问的并且可以使用,但是如果要使用具有相同名称的局部变量,请使用 var



在您的情况下,您只有一个同名的上下文。当你第一次介绍这个名字的时候,给它一个 var (或者不是,它没关系太多),只有一次。当您使用下面的相同名称时,您可以为其分配不同的对象(它可能是完全不同的类型),然后对前一个对象的访问将丢失(除非您将其保留在其他位置),并且该名称用于引用其他一些对象。



您可以将变量名称理解为对某个对象的访问权限。通过这种方式,您有两个对象:引用和引用的对象。您是否将新对象分配给变量,先前引用的对象保持不变但可能无法访问(因此最终可以将其删除)。您忘记此对象并使用相同的变量引用另一个对象。那是Javascript。 :-)



-SA
Just remove var; it is only needed to resolve ambiguity between variables given the same name in outer and inner context: in inner context, outer variable is accessible and can be used, but if you want to use a local variable with the same name, introduce it with var.

In your case, you have only one context for the same name. When you first introduce the name, give it with var (or not, it does not matter too much), only once. When you use the same name below, you assign different object to it (it could be of completely different type), and then the access to the previous object is lost (unless you preserve it somewhere else), and the name is used to reference some other object.

You can understand a variable name as an access to some object. In this way, you have two objects: the reference and the referenced object. Is you assign new object to a variable, the previously referenced object remains intact but can become inaccessible (so it could be removed eventually). You "forget" this object and reference another one with the same variable. That is Javascript. :-)

—SA


这篇关于如果我在Javascript验证中多次声明相同的变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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