简体   繁体   English

为什么这个JavaScript函数被调用两次?

[英]Why is this JavaScript function called twice?

In this sample from W3 School Array Sort, the function displayCars seems to be called 2 times. 在W3 School Array Sort中的此示例中,函数displayCars似乎被调用了2次。 It's called before the first function and inside the first function. 在第一个函数之前和第一个函数内部调用它。 Could someone explain the reasoning behind this? 有人可以解释这背后的原因吗? I'm having trouble understanding why. 我无法理解原因。 Thanks in advance. 提前致谢。

 var cars = [ {type:"Volvo", year:2016}, {type:"Saab", year:2001}, {type:"BMW", year:2010}] displayCars(); function myFunction() { cars.sort(function(a, b){return a.year - b.year}); displayCars(); } function displayCars() { document.getElementById("demo").innerHTML = cars[0].type + " " + cars[0].year + "<br>" + cars[1].type + " " + cars[1].year + "<br>" + cars[2].type + " " + cars[2].year; } 
 <div id="demo"></div> 

It's actually only being called once because the second call for displayCars() is inside of myFunction , which is never being called. 实际上,它仅被调用一次,因为对displayCars()的第二次调用在myFunction内部,而从未调用过。

Change the code to call myFunction and stay away from W3 Schools as it is well known to have incorrect and outdated information. 更改代码以调用myFunction ,并远离W3 Schools,因为众所周知它具有错误和过时的信息。 Use the Mozilla Developer Network instead . 请改用Mozilla开发人员网络

 var cars = [ {type:"Volvo", year:2016}, {type:"Saab", year:2001}, {type:"BMW", year:2010}] myFunction(); function myFunction() { cars.sort(function(a, b){return a.year - b.year}); displayCars(); } function displayCars() { document.getElementById("demo").innerHTML = cars[0].type + " " + cars[0].year + "<br>" + cars[1].type + " " + cars[1].year + "<br>" + cars[2].type + " " + cars[2].year; } 
 <div id="demo"></div> 

I is caalled twice to show the result before sorting and after sorting. 我被要求两次以显示排序之前和之后的结果。

You need to add the call of myFunction as well. 您还需要添加对myFunction的调用。

 function myFunction() { cars.sort(function(a, b){return a.year - b.year}); displayCars(); } function displayCars() { document.getElementById("demo").innerHTML += cars[0].type + " " + cars[0].year + "<br>" + cars[1].type + " " + cars[1].year + "<br>" + cars[2].type + " " + cars[2].year + '<hr>'; } var cars = [{ type: "Volvo", year: 2016 }, { type: "Saab", year: 2001 }, { type: "BMW", year: 2010 }]; displayCars(); // show content myFunction(); // sort and show content 
 <div id="demo"></div> 

它被称为一次,但是为什么在代码中两次提到它却是为了让您按原样以及对结果进行排序后查看结果。

The function is not called twice in your snippet. 您的代码段中未两次调用该函数。 I'm assuming there's more code we haven't seen, which calls myFunction ; 我假设还有更多我们尚未看到的代码,称为myFunction the purpose of calling the function twice would then be to show the effect of the sorting function. 调用该函数两次的目的将是显示排序函数的效果。 Assuming myFunction is called after the display function, the program would do the following: 假设在显示函数之后调用了myFunction ,程序将执行以下操作:

  • Display the cars in the (unsorted) order they start in 按开始的(未排序)顺序显示汽车
  • Run myFunction which sorts the cars then displays them again 运行myFunction对汽车进行排序,然后再次显示它们

This would allow the user to see the difference between the car list before and after it's sorted. 这将使用户可以查看排序前后的汽车列表之间的差异。

It is run only once. 它仅运行一次。 You are not call myFunction() function. 您没有调用myFunction()函数。 If you call it the array will sort. 如果调用它,数组将排序。 Try this one. 试试这个。

var cars = [
{type:"Volvo", year:2016},
{type:"Saab", year:2001},
{type:"BMW", year:2010}]

myFunction();

function myFunction() {
    cars.sort(function(a, b){return a.year - b.year});
    displayCars();
}

function displayCars() {
  document.getElementById("demo").innerHTML =
  cars[0].type + " " + cars[0].year + "<br>" +
  cars[1].type + " " + cars[1].year + "<br>" +
  cars[2].type + " " + cars[2].year;
}

myFunction() will sort array. myFunction()将对数组进行排序。 displayCars() will show array. displayCars()将显示数组。

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

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