简体   繁体   中英

Javascript: How can you get a single element out of an array and make a string from it

I am new to javascript and was trying something in an exercise for school.

I have an array with some javascript elements.

This is the array:

var testObjecten = [
    window.walkTheDog,
    window.focus,
    document.images,
    document.layers,
    document.all,
    document.getElementById,
    document.getElementsByTagName,
    document.styleSheets,
    document.createElement,
    document.createTreeWalker,
    document.implementation.createDocument,
    window.ActiveXObject,
    window.XMLHttpRequest
    ];

My objective is to test them for support in the browser and return yes or no. I know how to get to the correct result but I cannot do some things needed for it.

I have created a table with Javascript. In the first part of the table must come a TextNode with the full name of the element. So I was trying to convert the element to string but it does not seem to work.

I know you can convert an array to one big string with join but how can you do this with an element?

Also on a related question: If the same array would contain strings with the elements above (with "" around them) would it still be possible to test with them?

I know you can convert an array to one big string with join but how can you do this with an element?

Javascript does also know the method '.toString()'. Perhaps it will work.

Also on a related question: If the same array would contain strings with the elements above (with "" around them) would it still be possible to test with them?

In Javascript exists the method '.eval()'. This one is primitiv but very powerful.

for(var i=0; i<testObjecten.length;i++){
  if(testObjecten[i]==null){
     alert("not Supported");
  }
}

the objects are automatically converted to String using toString() method.

you can save names of your elements in separate array with same indexes and then output it.

I'm not quite sure what your attempting but this might help.

var testObjecten = {
    "window.walkTheDog": window.walkTheDog,
    "window.focus": window.focu,
    ...
};

var names = [];
for (var key in testObjecten) {
   o.push(key);
}

document.createTextNode(names[i]);

The problem with trying to use toString is that you get things like HTMLElement or other data you do not actually care about. I believe you wanted to print the strings "window.walkTheDog", etc.

There is no easy way of getting this data without having the physical strings somewhere.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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