官术网_书友最值得收藏!

Example JavaScript in action

With all of the JavaScript syntax specifications covered, let's use some of them into a working example and see what happens. Have a look at the following simple HTML document containing JavaScript to sort a randomized array of numbers:

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <title>Insertion Sort - JavaScript Syntax Example</title>

    <script type="text/javascript">
      // Number of elements to sort.
      elementCount = 10000;     
      // The array which will be sorted.
      sortlist = new Array();

      /**
      * Called on button click.
      */
      function init() {
        // Prepare random array for sorting.
        for(i = 0; i < elementCount; i++)
          sortlist.push(i);

        //shuffle(sortlist);
        sortlist.sort(function() {
          return 0.5 - Math.random();
        });

        // Display the random array prior to sorting.
        console.log(sortlist);

        // Start a timer.
        console.time('Iteration Sort Timer');

        // Sort the randomized array.
        insertionSort(sortlist);

        // Stop the timer.
        console.log('Sorted ' + elementCount + ' items.');
        console.timeEnd('Iteration Sort Timer');

        // Display the sorted array.
        console.log(sortlist);
      }

      /**
      * The popular Insertion Sort algorithm.
      */
      function insertionSort(list) {
        // It's always smart to only lookup array size once.
        l = list.length;

        // Loop over supplied list and sort.
        for(i = 0; i < l; i++) {
          save = list[i];
          j = i;

          while(j > 0 && list[j - 1] > save) {
            list[j] = list[j - 1];
            j -= 1;
          }

          list[j] = save;
        }
      }
    </script>
  </head>

  <body>
    <p>
      Click the button below to begin.
      Be sure to open up your browsers developer console.
    </p>
      <button onclick="init()">Start Sorting</button>
  </body>
</html>

This example covers many of the features and syntax specifications of JavaScript that we have just covered. Within our JavaScript block declared in the HTML document head tag, we have created two functions. The first function is our initiation method to prepare and run the application once it called. The second function contains the popular insertion sort algorithm, which will sort our randomized array of numbers. To enable both functions to use the same variable, we create elementCount and sortlist as global variables outside of each function's scope. Within the HTML body tag is a button element, which renders a typical form button element on the page and when a user clicks this button, the onclick handler calls the init function.

This example isn't flashy by any means but, as I mentioned above, it covers many of the different aspects of the JavaScript syntax specifications.

主站蜘蛛池模板: 梧州市| 宁城县| 岑巩县| 潜山县| 璧山县| 桃园县| 合阳县| 子长县| 庆安县| 巧家县| 蒙阴县| 和林格尔县| 榆社县| 凤山市| 友谊县| 武汉市| 密云县| 阆中市| 竹溪县| 玉溪市| 阿坝| 威海市| 离岛区| 长丰县| 英德市| 察雅县| 崇礼县| 韶关市| 墨竹工卡县| 额济纳旗| 芦溪县| 沙雅县| 高雄市| 南宫市| 宣武区| 新乐市| 方山县| 盖州市| 桦南县| 济源市| 芜湖市|