有没有关键字'大写'在javascript

is there any keyword 'capital' in javascript?

本文关键字:javascript 大写 关键字 有没有      更新时间:2023-09-26

我正在调用一个名为'capital()'的函数,但它不工作,'capita'正在工作。这是js中的关键字吗?

这里是代码查看它,测试它

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>HTML Select Element</title>
<script type="text/javascript">
function capita(){
alert('yes');
c = new Array("Islamabad", "Tehran", "Bejing", "New Delhi", "Kabul");
var i;
i = document.f1.country.selectedIndex;
document.f1.capital.value=c[i];
}
</script>
</head>
<body>
<h3>Countries and Capital</h3>
<form name="f1" method="post">
The capital of 
<select name="country" id="country" onChange="capita();">
<option selected>Pakistan
<option>Iran
<option>China
<option>India
<option>Afghanistan 
</select>
is <input type="text" name="capital" value="Islamabad"/> 
</form> 
</body>
</html>

capital是对输入元素的引用。

<input type="text" name="capital" value="Islamabad"/>

这是因为当您使用内联处理程序时,会将某些DOM元素插入到变量作用域链中。具体插入哪些元素在某种程度上取决于浏览器。

如果您将select元素的onchange处理程序更改为:

<select name="country" id="country" onChange="alert(capital);"> 

你会看到它发出警告:

"[object HTMLInputElement]"

…或者类似的。

<<p> JSFIDDLE演示/strong>

下面是一个更新的例子,它改变了你的内联处理程序,像这样:

<select name="country" id="country" onChange="alert('LOCAL: ' + capital + 
                                             ''n'nGLOBAL: ' + window.capital);"> 

现在它警告capitalwindow.capital。如果你在不同的浏览器中测试,你可能会得到不同的结果。

<<p> JSFIDDLE演示/strong>

铬显示:

LOCAL: [object HTMLInputElement]

GLOBAL: function capital() {}

不,capital不是JavaScript关键字。它既不在当前关键字列表中,也不在将来保留词列表中。(甚至不包括前一个规范中较长的列表)

你的代码的问题是,在一些浏览器中,所有的name值最终是全局的。当然,在全局作用域中声明的所有函数也是如此。因此,在这些浏览器上,您的函数capital和标记中带有name"大写"的元素之间存在冲突。改变它们中的任何一个都可以解决问题。Internet Explorer尤其存在这个问题。

一般来说,避免在全局作用域中添加任何代码符号(除非您正在编写一个库,在这种情况下,单个符号的唯一命名是可以接受的)。例如,您的代码根本不需要任何全局代码符号: HTML:

<form name="f1" method="post">
The capital of 
<select name="country" id="country">
  <option selected>Pakistan</option>
  <option>Iran</option>
  <option>China</option>
  <option>India</option>
  <option>Afghanistan</option>
</select>
is <input type="text" name="capital" value="Islamabad"> 
</form> 

JavaScript(仅与全局符号相关的更改;还有其他一些我也建议修改的地方):

// Everything within a scoping function
(function() {
  // The capitals
  var capitals = [
    "Islamabad",
    "Tehran",
    "Bejing",
    "New Delhi",
    "Kabul"
  ];
  // Hook the window#load event
  hookEvent(window, "load", handleLoad);
  // Handle the load event
  function handleLoad() {
    // Hook the "change" event on "country"
    hookEvent(
      document.getElementById("country"),
      "change",
      countryChanged
    );
  }
  // Handle a change of the country
  function countryChanged(event) {
    document.f1.capital.value = capitals[document.f1.country.selectedIndex];
  }
  // === Utility functions, don't need if you have a decent library
  function hookEvent(element, eventName, handler) {
    // Very quick-and-dirty, recommend using a proper library,
    // this is just for the purposes of the example.
    if (typeof element.addEventListener !== "undefined") {
      element.addEventListener(eventName, handler, false);
    }
    else if (typeof element.attachEvent !== "undefined") {
      element.attachEvent("on" + eventName, handler);
    }
    else {
      element["on" + eventName] = handler;
    }
  }
})();

现场演示