简体   繁体   English

意外的标识符 [object Object]

[英]Unexpected identifier [object Object]

var costBusRoute = 600;
var busRoute = 0;
var vehCountBus = 0;
var routesBus = [{number:0, vehCount:0}];
var maintBus = 50;

function newBusRoute() {
    busRoute += 1;
    routesBus.push({number:busRoute, vehCount:0});
    viewRoutes(routesBus, "busRoutes", busRoute);
}

function newBus(routenum) {
    vehCountBus += 1;
    routenum.vehCount += 1;
    capacity += 50;
    bank -= 600;
    checkBank();
    checkCapacity();
    eiv += 99;
    checkEIV();
    updateVehNum(routenum);
}

function viewRoutes(routelist, listhtml, routenum) {
    document.getElementById(listhtml).innerHTML = "";
    for (I in routelist) {
        nameList = "<li class='list'><button onclick='editRoute("+routelist+","+routelist[routenum]+")'>" + routelist[I].number + "</button></li>";
        document.getElementById(listhtml).innerHTML += nameList;
    }
}

function editRoute(routelist, routenum) {
    $("#mainContainer").fadeOut();
    document.getElementById("screen").innerHTML = (
        "<h1>Bus Route " + routelist[routenum].number + "</h1>" +
        "<div id='vehicleNum'><p>Vehicles on this route: " + routelist[routenum].vehCount + "</div>" +
        "<p><button onclick='newBus("+routelist[routenum].number+")'>+ Add Vehicle | £600</button><p>Maintenance: £50 per day" +
        "<p><button onclick='delBus("+routelist[routenum].number+")'>- Remove Vehicle</button>" +
        "<p><button onclick='delRoute("+routelist[routenum].number+")'>- Delete Route</button>"
    );
}

I have searched for hours but haven't been able to work out why this throws an error or how to resolve it.我已经搜索了几个小时,但无法弄清楚为什么会引发错误或如何解决它。 Any advice is greatly appreciated!任何意见是极大的赞赏!

I'm trying to send the right element from routesBus to editRoute() so that it can edit the key values in that specific element.我正在尝试将正确的元素从routesBus发送到editRoute()以便它可以编辑该特定元素中的键值。 The error occurs at editRoute() and is shown in the console as Uncaught SyntaxError: Unexpected identifier .错误发生在editRoute()并在控制台中显示为Uncaught SyntaxError: Unexpected identifier

I think there are various problems:我认为有各种各样的问题:

  1. As stated in the comments, you try to add the array as a parameter just by concatenating it directly...and the rest of the parameters as well.如评论中所述,您尝试将数组添加为参数,只需将其直接连接……以及其余参数。
nameList = "<li class='list'><button onclick='editRoute("+routelist+","+routelist[routenum]+")'>" + routelist[I].number + "</button></li>";

All you get is is string like :你得到的只是像这样的字符串:

<li class='list'><button onclick='editRoute([object Object],[object Object]...
  1. In addition to that, editRoute expects an array ( routelist ) and a number ( routenum ), the index of the item in the array and when you call it, you (attempt to) give the array and the item, not the index.除此之外, editRoute需要一个arrayroutelist )和一个numberroutenum ),数组中项目的索引,当你调用它时,你(尝试)给出数组和项目,而不是索引。

  2. In viewRoutes you use both the index routenum given as parameter and the index I in the same line.viewRoutes您在同一行中同时使用作为参数给出的索引routenum和索引I This is inconsistent.这是不一致的。 You should give up the first one since you are building the whole list and the routenum index seems to be useless here.您应该放弃第一个,因为您正在构建整个列表并且routenum索引在这里似乎没有用。 It could be useful if when building the list, you would like to do something special when I == routenum (set the text in bold, use a specil color or icon...).如果在构建列表时您想在I == routenum时做一些特别的I == routenum (将文本设置为粗体,使用特殊颜色或图标...),这可能很有用。

To fix 1 & 2, I would rather use the index as the unique parameter of your editRoute function because you can build the html calling it more easily and because the parameter routelist corresponds to routesBus which is available from editRoute .要解决1&2,我宁愿使用索引作为你的唯一参数editRoute功能,因为你可以建立HTML更容易,因为参数调用它routelist对应于routesBus它可从editRoute

var costBusRoute = 600;
var busRoute = 0;
var vehCountBus = 0;
var routesBus = [{number:0, vehCount:0}];
var maintBus = 50;

function newBusRoute() {
    busRoute += 1;
    routesBus.push({number:busRoute, vehCount:0});
    viewRoutes("busRoutes");
}

function newBus(routenum) {
    vehCountBus += 1;
    routenum.vehCount += 1;
    capacity += 50;
    bank -= 600;
    checkBank();
    checkCapacity();
    eiv += 99;
    checkEIV();
    updateVehNum(routenum);
}

function viewRoutes(listhtml) {
    let nameList  = "";
    for (I in routelist) {
        nameList += `<li class='list'><button onclick='editRoute(${routenum})'>${routesBus[I].number}</button></li>`;

    }
    document.getElementById(listhtml).innerHTML = nameList;
}

function editRoute(routenum) {
    $("#mainContainer").fadeOut();
    let route = routesBus[routenum];
    let number = route.number; //isn't always route.number === routenum?
    // TODO use template literal for the following
    document.getElementById("screen").innerHTML = (
        "<h1>Bus Route " + number + "</h1>" +
        "<div id='vehicleNum'><p>Vehicles on this route: " + route.vehCount + "</div>" +
        "<p><button onclick='newBus("+ number +")'>+ Add Vehicle | £600</button><p>Maintenance: £50 per day" +
        "<p><button onclick='delBus("+ number + ")'>- Remove Vehicle</button>" +
        "<p><button onclick='delRoute(" + number  + ")'>- Delete Route</button>"
    );
}

Note that I used template literals on viewRoutes, you should use it as often as you can.请注意,我在 viewRoutes 上使用了模板文字,您应该尽可能多地使用它。 Check lso the other places I have changed.检查我已经改变的其他地方。 Another thing: since you are using JQuery, your rather learn it a bit, especially .click() and how to append elements with it, which would be a big improvement.另一件事:由于您使用的是 JQuery,您宁愿学习它,尤其是.click()以及如何使用它附加元素,这将是一个很大的改进。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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