Home  >  Article  >  Web Front-end  >  JavaScript string method toSource() that returns the source code of an object

JavaScript string method toSource() that returns the source code of an object

黄舟
黄舟Original
2017-11-04 13:19:531749browse

Definition and Usage

The toSource() method returns a string representing the source code of the object.

Syntax

object.toSource()

Tips and Notes

Note: This method does not work in Internet Explorer.

Example

The following example shows you the usage of toSource() method:

<script type="text/javascript">

function employee(name,job,born)
{
this.name=name;
this.job=job;
this.born=born;
}

var bill=new employee("Bill Gates","Engineer",1985);

document.write(bill.toSource());

</script>

Output:

({name:"Bill Gates", job:"Engineer", born:1985})

Returns the source code representing the array string.
Example:

<html>
<head>
<title>JavaScript Array toSource Method</title>
</head>
<body>
<script type="text/javascript">
var arr = new Array("orange", "mango", "banana", "sugar");
 
var str = arr.toSource();
document.write("Returned string is : " + str );
 
</script>
</body>
</html>

This will produce the following results:

Returned string is : ["orange", "mango", "banana", "sugar"]


The above is the detailed content of JavaScript string method toSource() that returns the source code of an object. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn