本文介绍 JavaScript 中 Boolean.toSource() 方法,该方法返回表示对象的源代码字符串。需注意此方法非标准且不支持 Internet Explorer。通过示例展示了如何定义对象并使用该方法输出其源码形式,结果呈现为包含属性值的对象字面量格式。
toSource() 方法概述
JavaScript 中 Boolean.toSource() 方法用于生成一个字符串,该字符串包含表示当前对象的源代码。
浏览器兼容性注意
需要注意的是,此方法并非标准 ECMAScript 的一部分,因此在 Internet Explorer 浏览器中无法正常运行。
boolean.toSource()
参数说明
该方法不接受任何参数。
- 无需参数。
返回值定义
调用此方法后,将返回一个字符串,内容为表示该对象的源代码。
代码示例与结果
以下示例演示了具体用法:
<html>
<head>
<title>JavaScript toSource() Method</title>
</head>
<body>
<script type="text/javascript">
function book(title, publisher, price)
{
this.title = title;
this.publisher = publisher;
this.price = price;
}
var newBook = new book("Perl","Leo Inc",200);
document.write(newBook.toSource());
</script>
</body>
</html>
执行上述代码后,浏览器控制台或页面将输出如下结果:
({title:"Perl", publisher:"Leo Inc", price:200})