2

Basically i want to print all properties on the window object, i am using:

for(var k in window){
document.write("window object = "+ Object.getOwnPropertyNames(window[k])+ "<br>");
}

I get a weird output with that code.I want to print each property on a new line using for in,how can i do that? thanks!

MrShabana
  • 377
  • 1
  • 4
  • 12
  • if you're using modern JS, be aware that `for/in` is a legacy syntax construction that returns an almost guaranteed unreliable set of values. If you must, use the modern `for/of`, but really you should look at at `Object.keys` and `Object.getOwnProperties`. Also, `document.write` is a truly ancient and extremely dangerous function and should not be used in new code. Use `console.log` if you just want to "see" stuff, or use the DOM API (creating nodes and appending them to some element, or the body) if you need inject things into your page. – Mike 'Pomax' Kamermans Jan 04 '17 at 17:57

4 Answers4

4

Why are you using for/in, and why are you using document.write?

The first comes to us from legacy JavaScript, from a time before we had proper object key iteration baked right into the JavaScript language, and because it is a bad idea(tm) depending on what you're iterating, with more appropriate modern functions available, should not be used anymore. The second is an ancient low level function that absolutely does not do what you think it does and should never be used in modern code. It is incredibly dangerous, and any tutorial that still uses it that told you to use it should be ashamed of itself.

So: solve your problem with modern JS, not legacy or even obsolete JS. To get all the keys for an object, use Object.keys(...) and use the Console API (console.log, etc.) if you just want to see what's in your object.

Object.keys(window).forEach( key => {
  let type =  typeof window[key];
  console.log(`${key} (${type})`);
});

Done.

Every window property and function listed on its own line, with the type listed because you usually want to know about that. Also note that array.forEach call: JavaScript has a ton of super useful array functions, well worth giving the available functions a quick read through. Finally, this code uses an arrow function, which is a rather nice new piece of JS syntax. You don't have to use it, but it's super compact without sacrificing readability. It also uses a template literals rather than string literals, which are another very handy modern JS feature. The above code and the follow code are equivalent in this case:

Object.keys(window).forEach( function(key) {
  var type =  typeof window[key];
  console.log(key + " (" + type + ")");
});

(they are not equivalent if we use a one-liner arrow function, or the arrow function body uses this - again, well worth giving the explanation of arrow functions a quick read-through)

This covers all enumerable properties, which is typically all you want. In the extremely unlikely case you need enumerable and non-enumerable properties, you use Object.getOwnProperties(...).

Mike 'Pomax' Kamermans
  • 49,297
  • 16
  • 112
  • 153
  • Note: Sometimes `document.write()` is the only option. E.G. working with the Internet Explorer COM object. – Sancarn Oct 04 '18 at 09:26
  • Sure, if you still live in 1995 and you think having IE work with COM objects makes any kind of sense, `document.write` is from exactly when you are in time. But it's 2018, IE is officially deprecated, and talking to COM objects directly from a browser hasn't been a thing for over a decade on top of that. So to repeat myself, with added emphasis, it "should **never** be used in _modern code_" – Mike 'Pomax' Kamermans Oct 04 '18 at 15:05
  • Hmm... How else would you talk to a native application from the browser other than via a COM object? I get that you can use http requests, but I have found that to be really unstable in the past (sometimes not even working at all). Where as with COM objects it's easy... Anyway, in general, it depends on how flexible your IT systems are. If you can run anything with freedom, then sure, I agree. If you are limited, like I have been in the past, it may be your only option. Also note: IE11 is still supported by MS – Sancarn Oct 06 '18 at 14:55
  • I didn't say it wasn't supported, I said it was deprecated. Which it is: IE11 will only ever receive fixes for vulnerabilities until it goes EOL. IE as a product has been discontinued. As for how I'd talk to native applications: I wouldn't. two decades of attack surfaces is long enough to demonstrate how terrible an idea that is. If you need to show things using a web interface, do it properly and have the server talk to anything native, because it can, and the browser never should. – Mike 'Pomax' Kamermans Oct 06 '18 at 16:16
1

Object.getOwnPropertyNames(window) should give you all the property names owned by the window object. Then for printing all the names in console you can make use of forEach method on array.

Object.getOwnPropertyNames(window).forEach(function(currentValue){console.log(currentValue)});
Abhinav Galodha
  • 9,293
  • 2
  • 31
  • 41
0

You could do it like this, getOwnPropertyNames returns an array, you could iterate that using forEach.

Object.getOwnPropertyNames(window)
    .forEach(function(object) {
            document.writeln(object+"<br/>");
    });
azs06
  • 3,467
  • 2
  • 21
  • 26
0

if you want to print all properties inside a div in HTML:

var div = document.createElement("div");
document.body.appendChild(div);

var obj = window;

do Object.getOwnPropertyNames(obj).forEach(function(name) {
    div.innerHTML = div.innerHTML + name + "<br/>";
  });
while(obj = Object.getPrototypeOf(obj));

some piece of this code was taken from another stackoverflow answer

Community
  • 1
  • 1
tfidelis
  • 443
  • 8
  • 16