2015-06-20 119 views
-1

我遇到了我的代码问题。 没有发生函数f和g不叫,我只是想传递数组到一个函数,做的东西吧:功能在javascript中不起作用

<html> 
    <head> 
    <title>test</title> 
    </head> 
    <body> 
     <script lang="JavaScript" type="text/javascript"> 
     // l = prompt("Your name :"); // if i remove the comment it works 
      function f(E) { 


       l = prompt("Your name :"); 
       E["Name"] = l; 
       l = prompt("Your Age :"); 
       E["Age"] = l; 
       l = prompt("Your Note :"); 
       E["Note"] = l; 

      } 
     // l = prompt("Your name :"); // if i remove the comment it works 
      function g(E) { 
       for (ind in E) { 

        document.write("E[" + ind + "]=" + E[ind]); 
       } 
      } 
      E = newarray(3); 
      //l = prompt("Your name :"); // here if i remove the comment nothing happen 
      f(E); 
      g(E); 
</script> 

    </body> 
</html> 
+3

您的代码使用的是一个名为'newarray'的函数,它在您所显示的代码中没有定义。如果你看看你的Web控制台,你会看到一个很好的,明确的错误信息,指向失败的那一行。 –

+2

“没有任何事情”是不完全正确的(也语法;-)) - 您的代码中有一个错误,防止进一步执行。你应该看看JS控制台(不同的方式来访问它取决于浏览器)看到err-msg .... – MBaas

回答

1

你不想数组可言,你要的对象。您可以使用对象初始化创建对象:

E = {}; 

附注1:您的代码是所有的地方,以The Horror of Implicit Globals堕入。你需要声明你的变量。


备注2;在页面的主要解析完成后(例如,在prompt之后),将使用document.write隐式调用document.open,这将完全清除您的页面。如果你想添加到页面,使用DOM。


下面是与清理代码的各种问题,包括变量的命名和功能命名  —有意义的名称,是人们试图帮助您对您有用,而有用的例子:

function getData() { 
 
    var data = {}; 
 
    data.Name = prompt("Your name :"); 
 
    data.Age = prompt("Your Age :"); 
 
    data.Note = prompt("Your Note :"); 
 
    return data; 
 
} 
 

 
function showData(data) { 
 
    for (var key in data) { 
 
    display("E[" + key + "]=" + data[key]); 
 
    } 
 
} 
 

 
function display(msg) { 
 
    var p = document.createElement('p'); 
 
    p.innerHTML = msg; 
 
    document.body.appendChild(p); 
 
} 
 

 
var d = getData(); 
 
showData(d);

+0

请你能告诉我为什么我不能使用document.write? – cip

+0

@cip:我做了:*“使用'document.write' ...会隐式地调用'document.open',它将彻底清除你的页面...”*通过“清除”我的意思是它将删除所有的网页内容并将其替换。我想你的当前页面没有任何内容,所以这样可以,但是在完成主解析之后,通常是'document.write'是一个禁忌。 (有些人认为它总是*一个禁忌。) –

0

使用new Array(3)代替newarray(3)

<html> 
 
    <head> 
 
    <title>test</title> 
 
    </head> 
 
    <body> 
 
     <script lang="JavaScript" type="text/javascript"> 
 
     // l = prompt("Your name :"); // if i remove the comment it works 
 
      function f(E) { 
 

 

 
       l = prompt("Your name :"); 
 
       E["Name"] = l; 
 
       l = prompt("Your Age :"); 
 
       E["Age"] = l; 
 
       l = prompt("Your Note :"); 
 
       E["Note"] = l; 
 

 
      } 
 
     // l = prompt("Your name :"); // if i remove the comment it works 
 
      function g(E) { 
 
       for (ind in E) { 
 

 
        document.write("E[" + ind + "]=" + E[ind]); 
 
       } 
 
      } 
 
      E = new Array(3); 
 
      //l = prompt("Your name :"); // here if i remove the comment nothing happen 
 
      f(E); 
 
      g(E); 
 
</script> 
 

 
    </body> 
 
</html>

在这里,您可能需要使用对象而不是数组。

<html> 
 
    <head> 
 
    <title>test</title> 
 
    </head> 
 
    <body> 
 
     <script lang="JavaScript" type="text/javascript"> 
 
     // l = prompt("Your name :"); // if i remove the comment it works 
 
      function f(E) { 
 

 

 
       E["Name"] = prompt("Your name :"); 
 
       E["Age"] = prompt("Your Age :"); 
 
       E["Note"] = prompt("Your Note :"); 
 

 
      } 
 
     // l = prompt("Your name :"); // if i remove the comment it works 
 
      function g(E) { 
 
       for (ind in E) { 
 

 
        document.write("E[" + ind + "]=" + E[ind]); 
 
       } 
 
      } 
 
      var E = {}; 
 
      //l = prompt("Your name :"); // here if i remove the comment nothing happen 
 
      f(E); 
 
      g(E); 
 
</script> 
 

 
    </body> 
 
</html>

+0

除非OP实际上并不需要数组,否则没有任何理由在它不用于存储索引0,1或2处的任何内容时给它3的长度。 –

+0

我同意。我只是指出了错误。 – Vikash

0

建议:

首先,它应该是E =新阵列(3); 由于E是一个全局变量(并在第一个函数中使用),因此最好在第一个函数之前声明它。 虽然没有必要,但在调试时为每个提示提供自己的变量名将会有所帮助。