Three ways to return multiple values in the JS method

Source: Internet
Author: User

In the use of JS programming, sometimes you need to return two or more than two data in a method, the following methods can be implemented:

1 Use the array in the following way:

<HTML><Head>    <title>The JS function returns multiple values--oec2003</title></Head><Body>    <inputtype= "button"onclick= "GetNames ()"value= "Test" />    <Scripttype= "Text/javascript">functionGetData () {varnames=NewArray ("oec2003","oec2004"); returnnames;}functionGetNames () {varnames=GetData (); Alert (GetData () [0]); //back to oec2003}</Script></Body></HTML>

2 encapsulate the data in JSON and return it as follows:

<HTML><Head><title>The JS function returns multiple values--oec2003</title></Head><Body><inputtype= "button"onclick= "GetInfo ()"value= "Test"/><Scripttype= "Text/javascript">functionGetData () {varInfo={"name":"oec2003"," Age":" -"}; returninfo;}functionGetInfo () {varInfo=GetData (); varname=info["name"]; var Age=info[" Age"]; Alert ("Name:"+name+"Age:"+Age );}</Script></Body></HTML>

For a more detailed introduction to JSON, see here

3 This is the simplest way to look at the following code:

<HTML><Head><title>The JS function returns multiple values--oec2003</title></Head><Body><inputtype= "button"onclick= "GetInfo ()"value= "Test"/><Scripttype= "Text/javascript">    functionGetData () {return ["oec2003",  -]    }    functionGetInfo () {varInfo=GetData (); Alert ("Name:" +info[0] + "Age:" +info[1]); }</Script></Body></HTML>

Article from: http://www.cnblogs.com/oec2003/archive/2009/12/11/1621775.html

Three ways to return multiple values in the JS method

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.