Javascript提供未定义的消息,而不是ex.message

Javascript提供未定义的消息,而不是ex.message,javascript,Javascript,下面的代码有效,仅当我在提示符()中键入字母时,我得到的是未定义的消息,而不是catch(ex)中的错误值ex.message。我已经试过很多次了,但我还是不知道怎么了。如何正确地执行此操作 var myList=[“橙子”、“苹果”、“菠萝”、“香蕉”]; var getFruit=函数(索引){ 如果(索引>myList.length | |索引myList.length | | indexmyList.length | | indexmyList.length | | indexmyLi

下面的代码有效,仅当我在
提示符()
中键入字母时,我得到的是未定义的消息,而不是
catch(ex)
中的错误值
ex.message
。我已经试过很多次了,但我还是不知道怎么了。如何正确地执行此操作

var myList=[“橙子”、“苹果”、“菠萝”、“香蕉”];
var getFruit=函数(索引){
如果(索引>myList.length | |索引<0){

抛出new RangeError(“您给出的数字在列表中不存在,数字必须为0首先检查isNaN…将getFruit更改为以下内容

var getFruit = function(index) {
    if(isNaN(index)) {
        throw new WrongValue("Give a number please");
    else if(index > myList.length || index < 0) {
        throw new RangeError("The number you gave doesn't exist in the list, the number must be 0 <= # <= " + myList.length);
    } else {
        return myList[index];
    }

}
var getFruit=函数(索引){
if(isNaN(索引)){
抛出新的错误值(“请给出一个数字”);
else if(index>myList.length | | index<0){

抛出new RangeError(“您给出的数字在列表中不存在,数字必须为0首先检查isNaN…将getFruit更改为以下内容

var getFruit = function(index) {
    if(isNaN(index)) {
        throw new WrongValue("Give a number please");
    else if(index > myList.length || index < 0) {
        throw new RangeError("The number you gave doesn't exist in the list, the number must be 0 <= # <= " + myList.length);
    } else {
        return myList[index];
    }

}
var getFruit=函数(索引){
if(isNaN(索引)){
抛出新的错误值(“请给出一个数字”);
else if(index>myList.length | | index<0){

抛出新的RangeError(“您给出的数字在列表中不存在,数字必须为0,如所写,现在总是要检查您的一个条件,因为它们返回了
throw
这两个条件都会阻止代码继续执行下一条指令,所以请尝试将这两个条件合并在一个条件中,并检查
isNaN()
首先,它像:

var getFruit = function(index) {
  if (isNaN(index)) {
    throw new WrongValue("Give a number please");
  } else if (index > myList.length || index < 0) {
    throw new RangeError("The number you gave doesn't exist in the list, the number must be 0 <= # <= " + myList.length);
  } else {
    return myList[index];
  }
}
var getFruit=函数(索引){
if(isNaN(索引)){
抛出新的错误值(“请给出一个数字”);
}else if(index>myList.length | | index<0){

抛出新的RangeError(“您给出的数字在列表中不存在,数字必须为0,如所写,现在总是要检查您的一个条件,因为它们返回了
throw
这两个条件都会阻止代码继续执行下一条指令,所以请尝试将这两个条件合并在一个条件中,并检查
isNaN()
首先,它像:

var getFruit = function(index) {
  if (isNaN(index)) {
    throw new WrongValue("Give a number please");
  } else if (index > myList.length || index < 0) {
    throw new RangeError("The number you gave doesn't exist in the list, the number must be 0 <= # <= " + myList.length);
  } else {
    return myList[index];
  }
}
var getFruit=函数(索引){
if(isNaN(索引)){
抛出新的错误值(“请给出一个数字”);
}else if(index>myList.length | | index<0){

抛出新的RangeError(“列表中不存在您提供的号码,号码必须为0检查
isNaN
getFruit
中的第一个,将另一个if/else放在isNaN测试的else中。不清楚您正在尝试做什么,以及您遇到了什么错误。请使用“编辑”按钮链接以更清楚地说明从何处获得此“未定义的错误消息”但是,有两件事突然向我袭来:1.你永远不会执行
getFruit
中的第二个
if/else
块,因为第一个
if/else
会抛出一个异常或返回一个值,这两个都会退出函数。2.你根本不会用
getFruit
的返回值做任何事情。检查
 isNaN
首先在
getFruit
中将另一个if/else放在isNaN测试的else中它不清楚您试图做什么以及您遇到了什么错误。请使用“编辑”链接更清楚地说明您从何处收到此“未定义的错误消息”但是,有两件事突然向我袭来:1.你永远不会执行
getFruit
中的第二个
if/else
块,因为第一个
if/else
会抛出一个异常或返回一个值,这两个都会退出函数。2.你根本不会用
getFruit
的返回值做任何事情。