zoukankan      html  css  js  c++  java
  • js方法返回多值如何取值demo

    js方法返回,如何取值?下面demo两种方法 new array 和 json 返回值 取值示例.

    方法一:  new array

     1 <html>
     2 <head>
     3     <meta charset='UTF-8'>
     4     <title>方法一: New Array</title>
     5 </head>
     6 <body>
     7     <input type='button' onclick='getInfo()' value='测试按钮'/>
     8 </body>
     9 <script type='text/javascript'>
    10     function getData(){
    11         return  new Array('admin_one','Hello World One');
    12         //return  ['Admin_One','Hello World One'];  
    13     }
    14     function getInfo(){
    15         var data=getData();
    16         alert(data[0]); //admin_one
    17         alert(getData()[1]); //Hello World One
    18     }
    19 </script>
    20 </html> 

     结果打印:

    方法二:  json

     1 <html>
     2 <head>
     3     <meta charset='UTF-8'>
     4     <title>方法二: Josn</title>
     5 </head>
     6 <body>
     7     <input type='button' onclick='getInfo()' value='测试按钮'/>
     8 </body>
     9 <script type='text/javascript'>
    10     function getData(){
    11         return {'name':'admin_two','content':'Hello World Two'};
    12     }
    13     function getInfo()
    14     {
    15         var data=getData();
    16         alert(data.name); //取值方法一: admin_two
    17         alert(getData().content); //取值方法一:Hello World Two
    18         
    19         alert(data['name']); //取值方法二:admin_two
    20         alert(getData()['content']); //取值方法二:Hello World Two
    21 
    22     }
    23 </script>
    24 </html> 

     结果打印:

  • 相关阅读:
    iOS开发-UIScrollView原理
    关于tableView点击状态栏列表回到顶部的说明
    实用技术之bugly
    KVO你所不知道的"坑"
    iOS iPhone SDK 包含哪些东西?
    NSTime的全面认识
    四 数据结构与算法总结(一)
    三 数据结构 --数和二叉树
    二 线性表
    一 数据结构的概念,时间复杂度和空间复杂度
  • 原文地址:https://www.cnblogs.com/cxx8181602/p/10214878.html
Copyright © 2011-2022 走看看