<legend id='ke1Tg'><style id='ke1Tg'><dir id='ke1Tg'><q id='ke1Tg'></q></dir></style></legend>
    • <bdo id='ke1Tg'></bdo><ul id='ke1Tg'></ul>

  • <tfoot id='ke1Tg'></tfoot>

    <small id='ke1Tg'></small><noframes id='ke1Tg'>

        <i id='ke1Tg'><tr id='ke1Tg'><dt id='ke1Tg'><q id='ke1Tg'><span id='ke1Tg'><b id='ke1Tg'><form id='ke1Tg'><ins id='ke1Tg'></ins><ul id='ke1Tg'></ul><sub id='ke1Tg'></sub></form><legend id='ke1Tg'></legend><bdo id='ke1Tg'><pre id='ke1Tg'><center id='ke1Tg'></center></pre></bdo></b><th id='ke1Tg'></th></span></q></dt></tr></i><div id='ke1Tg'><tfoot id='ke1Tg'></tfoot><dl id='ke1Tg'><fieldset id='ke1Tg'></fieldset></dl></div>

        打印/显示 JavaScript 变量的名称而不是它的值

        时间:2023-08-07
        • <legend id='Xyy6U'><style id='Xyy6U'><dir id='Xyy6U'><q id='Xyy6U'></q></dir></style></legend>
            <tbody id='Xyy6U'></tbody>

          <small id='Xyy6U'></small><noframes id='Xyy6U'>

            <i id='Xyy6U'><tr id='Xyy6U'><dt id='Xyy6U'><q id='Xyy6U'><span id='Xyy6U'><b id='Xyy6U'><form id='Xyy6U'><ins id='Xyy6U'></ins><ul id='Xyy6U'></ul><sub id='Xyy6U'></sub></form><legend id='Xyy6U'></legend><bdo id='Xyy6U'><pre id='Xyy6U'><center id='Xyy6U'></center></pre></bdo></b><th id='Xyy6U'></th></span></q></dt></tr></i><div id='Xyy6U'><tfoot id='Xyy6U'></tfoot><dl id='Xyy6U'><fieldset id='Xyy6U'></fieldset></dl></div>

            <tfoot id='Xyy6U'></tfoot>

                <bdo id='Xyy6U'></bdo><ul id='Xyy6U'></ul>

                1. 本文介绍了打印/显示 JavaScript 变量的名称而不是它的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  是否可以打印/显示 JavaScript 变量的名称?例如:

                  Is it possible to print / display a JavaScript variable's name? For example:

                  var foo=5;
                  var bar=6;
                  var foobar=foo+bar;
                  
                  document.write(foo+ "<br>");
                  document.write(bar+ "<br>");
                  document.write(foobar + "<br>");
                  

                  我们将如何打印变量的名称以便输出如下:

                  How would we print the variable's names so the output would be:

                  foo 
                  bar 
                  foobar
                  

                  而不是:

                  5
                  6
                  11
                  

                  推荐答案

                  你可以把变量放在 object 然后以这种方式轻松打印它们:http:///jsfiddle.net/5MVde/7/

                  You can put the variables in an object then easily print them this way: http://jsfiddle.net/5MVde/7/

                  一切见 fiddle,这是 JavaScript...

                  See fiddle for everything, this is the JavaScript...

                  var x = {
                      foo: 5,
                      bar: 6,
                      foobar: function (){
                          var that=this;
                          return that.foo+that.bar
                      }
                  };
                  
                  var myDiv = document.getElementById("results");
                  
                  myDiv.innerHTML='Variable Names...';
                  for(var variable in x)
                  {
                      //alert(variable);
                      myDiv.innerHTML+='<br>'+variable;
                  }
                  
                  myDiv.innerHTML+='<br><br>And their values...';
                  myDiv.innerHTML+='<br>'+x.foo+'<br>'+x.bar+'<br>'+x.foobar();
                  

                  JavaScript for...in 语句循环通过对象的属性.

                  The JavaScript for...in statement loops through the properties of an object.

                  如果您不希望 foobar 成为函数,则另一种变体(感谢@elclanrs):http://jsfiddle.net/fQ5hE/2/

                  Another variation (thanks @elclanrs) if you don't want foobar to be a function: http://jsfiddle.net/fQ5hE/2/

                  这篇关于打印/显示 JavaScript 变量的名称而不是它的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:Javascript:避免字符串到整数或浮点类型转换与空或 下一篇:如何在JavaScript中将数字的二进制表示从字符串转

                  相关文章

                  最新文章

                  • <bdo id='FogjO'></bdo><ul id='FogjO'></ul>

                    1. <small id='FogjO'></small><noframes id='FogjO'>

                    2. <tfoot id='FogjO'></tfoot>
                    3. <legend id='FogjO'><style id='FogjO'><dir id='FogjO'><q id='FogjO'></q></dir></style></legend>

                    4. <i id='FogjO'><tr id='FogjO'><dt id='FogjO'><q id='FogjO'><span id='FogjO'><b id='FogjO'><form id='FogjO'><ins id='FogjO'></ins><ul id='FogjO'></ul><sub id='FogjO'></sub></form><legend id='FogjO'></legend><bdo id='FogjO'><pre id='FogjO'><center id='FogjO'></center></pre></bdo></b><th id='FogjO'></th></span></q></dt></tr></i><div id='FogjO'><tfoot id='FogjO'></tfoot><dl id='FogjO'><fieldset id='FogjO'></fieldset></dl></div>