简体   繁体   English

带有示例的JS对象属性访问和迭代误解

[英]JS Object Property Access and Iteration Misunderstanding w/ Example

Got a wierd issue... the code below outputs into a table a list of SNMP object/values from whatever OID that you provide to be walked. 遇到了一个棘手的问题……下面的代码将来自您提供的所有OID的SNMP对象/值列表输出到表中。 The code works however, the variable 'jason' does not work as I would have thought. 代码有效,但是变量“ jason”不起作用。

First, I cannot access the 'jason' object from the Chrome console (console.log || console.dir). 首先,我无法从Chrome控制台(console.log || console.dir)访问“ jason”对象。

The second issue is that if I put a console.log(key) in the 'for in' loop it does not output anything. 第二个问题是,如果将console.log(key)放入“ for in”循环中,则不会输出任何内容。

Third issue is that if I use dot notation to access the jason properties in the for loop outputs 'undefined'. 第三个问题是,如果我使用点表示法访问for循环输出“未定义”中的jason属性。

The last issue is that I read from other Stack posts that an array like object should not use the 'for in' loop but when I change it to a regular for loop it does not work.... 最后一个问题是,我从其他Stack帖子中读到,对象之类的数组不应使用“ for in”循环,但是当我将其更改为常规for循环时,它将不起作用。

Its like my jason variable doesn't exist but it does and the script below works??? 就像我的jason变量不存在,但它确实存在,并且下面的脚本有效??? I think I am misunderstanding something core to the JS language. 我认为我误解了JS语言的核心。 Thanks in advance for any guidance. 在此先感谢您的指导。

document.getElementById('submit').addEventListener('click', function(){

document.getElementById('msg').innerHTML = '<br /><button class="btn btn-success" disabled="disabled"> <i class="fa fa-spinner fa-spin"></i> </button> Loading...';
request = new XMLHttpRequest();
var host = document.getElementById('host').value,
    comm = document.getElementById('comm').value || 'public',
    oid = document.getElementById('oid').value || '1.3.6.1.2.1.2.2';
var post_vars = 'host=' + encodeURIComponent(host);
post_vars += '&comm=' + encodeURIComponent(comm);
post_vars += '&oid=' + encodeURIComponent(oid);
request.open('POST', 'snmp_json.php');
request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
request.addEventListener('readystatechange', function() {
  if (request.readyState === 4 && request.status === 200) {
    var jason = JSON.parse(request.responseText);
    var output = '<table class="table table-hover">';
    output += '<thead><tr><th>Object</th><th>Value</th></tr></thead>';
    output += '<tbody>';
    for (key in jason) {
        output += '<tr><td>' + key + '</td>';
        output += '<td>' + jason[key] + '</td></tr>';
    }
    output += '</tbody></table>';
    document.getElementById('msg').innerHTML = output;
  };
});
request.send(post_vars);

});

The JSON object cannot be accessed by Chrome because it is a private object in the event handler. Chrome无法访问JSON对象,因为它是事件处理程序中的私有对象。 To access the object, declare JSON outside of the event handler above document.getElementById('submit') 要访问该对象,请在document.getElementById('submit')上方的事件处理程序外部声明JSON。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM