我如何提取多行字符串返回类型的值

How can i extract the values of a multipline string return type

本文关键字:字符串 返回类型 何提取 提取      更新时间:2023-09-26

我有一个函数,它将返回三个值。

function test(){
  var price = '10';
  var name = 'apple';
  var avialable = 'yes';
  var p = price+name+avialable; 
  return (p);
}
var test = test();
alert(test);

这是我的小提琴

http://jsfiddle.net/thkc0fpk/1/

请让他们知道如何做到这一点,(返回类型也可以改变,如果需要的话)

返回数组:

function test(){
  var price = '10';
  var name = 'apple';
  var available = 'yes';
  var p = [price, name, available]; 
  return (p);
}
var test = test();
console.log(test[0]);  // price

或对象:

function test(){
  var price = '10';
  var name = 'apple';
  var available = 'yes';
  var p = { price: price, name: name, available: available }; 
  return (p);
}
var test = test();
console.log(test.price); // test.xxx

为什么不直接返回一个数组呢?

function test(){
  var price = '10';
  var name = 'apple';
  var avialable = 'yes';
  var p = [];
  p.push(price);
  p.push(name);
  p.push(avialable); 
  return p;
}
var test = test();

那么你可以这样访问这个字符串:

alert(test[0]);

我相信这里可以返回一个对象,像这样:

function test(){
  var p = {
      price: '10',
      name: 'apple',
      available: 'yes'
  }; 
  return p;
}
var test = test();
console.log(test);

对象可以通过var price = test.price;或类似的方式访问。