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

时间:2015-08-06 07:21:44

标签: javascript

我有一个函数会返回三个值。

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/

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

3 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

为什么不简单地返回数组?

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]);

答案 2 :(得分:0)

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

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

可以使用var price = test.price;或类似的方式访问该对象。