Reputation: 20202
Is it possible to output the name of an object in javascript?
In the below script i pass the window
object to a function and output the attributes.
var Output = "";
function OutputAttributes(pObject)
{
var x = "";
for (var Attribute in pObject)
{
x = x + "<li>"+pObject+"." + Attribute + ": " + pObject[Attribute] + "</li>";
}
return x;
}
Output = OutputAttributes(window);
document.write("<h2>Attributes from Objekt <i> <\/i><\/h2>");
document.write("<ol>"+Output+"</ol>");
If i execute my above code, then i receive output like this:
[object Window].close: function close() { [native code] }
[object Window].stop: function stop() { [native code] }
[object Window].focus:function focus() { [native code] }
But i expected something like this:
window.focus:function focus() { [native code] }
Upvotes: 0
Views: 60
Reputation: 4302
<script>
var str ="[object Window].focus:function focus() { [native code] }";
str = str.replace("[object","");
strlast=str.replace("Window]","Window");
alert(strlast);
</script>
Upvotes: 0
Reputation: 36703
It's not possible in JavaScript, because arguments in this language are passed by value or by reference, not by name, so when variable is passed to function, its name is lost.
Upvotes: 5