简体   繁体   中英

JAVASCRIPT --> Why do I get the error message “Uncaught TypeError: number is not a function ”?

I wonder why I get the error message (Uncaught TypeError: number is not a function) , this is my bit of Code:

<script type = "text/javascript">
  function dataparse(points)
   {
      points = points.substr(0, points.length - 1);
      var linar = points.split("\n");
      // Break each point by line break
      var wrkar = [];
      var pntar = [];
      for (var i = 0; i < linar.length; i++)
      {
          wrkar = linar[i].split(",", 2);
          // Break each point into x and y
          pntar.push(new google.maps.LatLng(parseFloat(wrkar[0]), parseFloat(wrkar[1])));
      }
      return pntar;
  }


  var cd = [
  (30.40545181622765, -9.582610613708539)
  (30.405229737948233, -9.577975756530805)
  (30.40300892737293, -9.577546603088422)
  (30.402268645956614, -9.584241396789594)
  ];

  var df = dataparse(cd);
  alert('df');

(30.40545181622765, -9.582610613708539) evaluates to -9.582610613708539

Together with the next line, it's interpreted as

-9.577975756530805(30.405229737948233, -9.577975756530805)

Which of course fails because -9.577... is not a function.

  1. Your dataparse function expects a string, given points.substr(0,points.length - 1)
  2. you initialize cd as an array, but the parser tries to execute (num1, num2) (num3, num4) as a function (see GGG's answer for the reason).

The error raises because of cd variable. You use parentheses, however it is not corrent. Following your dataparse method cd variable should look like:

var cd = "30.40545181622765, -9.582610613708539\n"+
    "30.405229737948233, -9.577975756530805\n"+
    "30.40300892737293, -9.577546603088422\n"+
    "30.402268645956614, -9.584241396789594";

UPDATE : However, you can rewrite your code in the more nice form:

function dataparse(points) {
    var pntar = [];
    for (var i = 0; i < points.length; i++) {
        pntar.push(new google.maps.LatLng(points[i][0], points[i][1]));
    }
    return pntar;
}

var cd = [
    [30.40545181622765, -9.582610613708539],
    [30.405229737948233, -9.577975756530805],
    [30.40300892737293, -9.577546603088422],
    [30.402268645956614, -9.584241396789594]
    ];

var df = dataparse(cd);
alert(df);

You're points array isn't valid. You will have to use square brackets for the points itself and split them by commas

var cd = [
    [30.40545181622765, -9.582610613708539],
    [30.405229737948233, -9.577975756530805],
    [30.40300892737293, -9.577546603088422],
    [30.402268645956614, -9.584241396789594]
];

And with that you will also have to modify (simplify) your dataparse function to handle the array correctly

function dataparse(points) {
    var i,
        length,
        pntar = [];

    for (i = 0, length = points.length; i < length; i++) {
        pntar.push(new google.maps.LatLng(points[i][0], points[i][1]));
    }

    return pntar;
}

You are getting this, because every number in your brackets is evaluated, you should replace that with the proper argument which your function accepts... since you are using substr , but actually you want to pass an array you should add some code to your function as well :

function dataparse(pairs)
{
    for ( i=0; i < pairs.length ; i++ ) {
       var wrkar = pairs[i].split(", ",2);
       var pntar = [] ;
       pntar.push( new google.maps.LatLng(parseFloat(wrkar[0]), parseFloat(wrkar[1])) ); 
       return pntar;
    }    
}


var cd = [
        "30.40545181622765, -9.582610613708539",
        "30.405229737948233, -9.577975756530805",
        "30.40300892737293, -9.577546603088422",
        "30.402268645956614, -9.584241396789594"
];

var df = dataparse(cd);
alert('df');​

here is a jsfiddle http://jsfiddle.net/ptQfc/

1st misake:

var cd = [
            (30.40545181622765, -9.582610613708539)
(30.405229737948233, -9.577975756530805)
(30.40300892737293, -9.577546603088422)
(30.402268645956614, -9.584241396789594)
];

Javascript array has to be separated with commas and not with newlines!

You've misunderstood the syntax required and it's causing a whole host of errors, namely trying to interpret your variables as a function.

In Javascript, the var=[val,val2] construct creates an array. You're trying to treat the contents of the brackets as a string, and split it as such.

A better way to store the array would be:

var cd = [
    [30.40545181622765, -9.582610613708539],
    [30.405229737948233, -9.577975756530805],
    [30.40300892737293, -9.577546603088422],
    [30.402268645956614, -9.584241396789594]
];

Your function could then be written as:

function dataparse(linar) {
    var wrkar = [] ;
    var pntar = [] ;
    for (var i = 0; i < linar.length; i++) {
        wrkar = linar[i];                // Break each point into x and y
        pntar.push( new google.maps.LatLng(parseFloat(wrkar[0]),parseFloat(wrkar[1]))); 
    }
    return pntar ;
}    

as linar is an array of arrays, each time you loop through the array it sets the next array down as the value of wrkar , which gives you the x and y coordinates.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM