JavaScript:将对象名称作为字符串 [英] JavaScript: alert object name as a string

查看:170
本文介绍了JavaScript:将对象名称作为字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在函数中,我试图以任何JavaScript对象作为字符串来 alert 。这意味着如果赋给函数的参数是 window.document ,实际的对象应该将window.document(不带引号)提醒为 / em> string。

I'm trying to alert any JavaScript object as a string, in a function. This means if the parameter given to the function is window.document, the actual object, it should alert "window.document" (without quotes) as a literal string.

以下调用...

example(window);
example(window.document);
example(document.getElementById('something'));

...调用此功能...

...calling this function...


函数示例(o){/ *有一点帮助吗? * /}

function example(o) {/* A little help here please? */}

...应输出以下字符串...

...should output the following strings...

window
window.document
document.getElementById('something')

我试图通过 toString() eval()在黑暗中的一些更多的杂项,没有成功。

I've attempted to do this with combinations of toString() and eval() among some more miscellaneous shots in the dark without success.

不需要疯狂的向后兼容性,更新的ECMAScript / JavaScript功能/功能很好。可以自由地询问澄清,尽管目标应该非常简单。

No need insane backwards compatibility, newer ECMAScript / JavaScript features/functions are fine. Feel free to inquire for clarifications though the goal should be pretty straight forward.

推荐答案

这是不可能做的自包含脚本。

This is not possible to do in a self contained script.

如果使用预处理器是一个选项,那么你可以编写一个转换 example(whatever)进入 example('whatever')。除此之外,我恐怕你没有运气。

If using a preprocessor would be an option, then you could write one which converts example(whatever) into example('whatever'). Other than that I'm afraid you're out of luck.

这篇关于JavaScript:将对象名称作为字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆