zoukankan      html  css  js  c++  java
  • 吴裕雄--天生自然PHP-MySQL-JavaScript学习笔记:JavaScript函数、对象和数组

    <script>
      function User(forename, username, password)
      {
        this.forename = forename
        this.username = username
        this.password = password
        this.showUser = showUser
      }
    
      function showUser()
      {
        document.write("Forename: " + this.forename + "<br>")
        document.write("Username: " + this.username + "<br>")
        document.write("Password: " + this.password + "<br>")
      }
    </script>
    <script>
      function User(forename, username, password)
      {
        this.forename = forename
        this.username = username
        this.password = password
    
        User.prototype.showUser = function()
        {
          document.write("Forename: " + this.forename + "<br>")
          document.write("Username: " + this.username + "<br>")
          document.write("Password: " + this.password + "<br>")
        }
      }
    </script>
    <script>
      numbers = []
      numbers.push("One")
      numbers.push("Two")
      numbers.push("Three")
    
      for (j = 0 ; j < numbers.length ; ++j)
        document.write("Element " + j + " = " + numbers[j] + "<br>")
    </script>
    <script>
      balls = {"golf":   "Golf balls, 6",
               "tennis": "Tennis balls, 3",
               "soccer": "Soccer ball, 1",
               "ping":   "Ping Pong balls, 1 doz"}
    
      for (ball in balls)
        document.write(ball + " = " + balls[ball] + "<br>")
    </script>
    <script>
      checkerboard = Array(
        Array(' ', 'o', ' ', 'o', ' ', 'o', ' ', 'o'),
        Array('o', ' ', 'o', ' ', 'o', ' ', 'o', ' '),
        Array(' ', 'o', ' ', 'o', ' ', 'o', ' ', 'o'),
        Array(' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '),
        Array(' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '),
        Array('O', ' ', 'O', ' ', 'O', ' ', 'O', ' '),
        Array(' ', 'O', ' ', 'O', ' ', 'O', ' ', 'O'),
        Array('O', ' ', 'O', ' ', 'O', ' ', 'O', ' '))
    
      document.write("<pre>")
    
      for (j = 0 ; j < 8 ; ++j)
      {
        for (k = 0 ; k < 8 ; ++k)
          document.write(checkerboard[j][k] + " ")
    
        document.write("<br>")
      }
    
      document.write("</pre>")
    </script>
    <script>
      pets = ["Cat", "Dog", "Rabbit", "Hamster"]
      pets.forEach(output)
    
      function output(element, index, array)
      {
        document.write("Element at index " + index + " has the value " +
          element + "<br>")
      }
    </script>
    <script>
      pets = ["Cat", "Dog", "Rabbit", "Hamster"]
    
      document.write(pets.join()      + "<br>")
      document.write(pets.join(' ')   + "<br>")
      document.write(pets.join(' : ') + "<br>")
    </script>
    <script>
      sports = ["Football", "Tennis", "Baseball"]
      document.write("Start = "      + sports +  "<br>")
    
      sports.push("Hockey");
      document.write("After Push = " + sports +  "<br>")
    
      removed = sports.pop()
      document.write("After Pop = "  + sports +  "<br>")
      document.write("Removed = "    + removed + "<br>")
    </script>
    <script>
      numbers = []
    
      for (j=1 ; j<6 ; ++j)
      {
        if (j == 2 || j == 5)
        {
          document.write("Processing 'todo' #" + j + "<br>")
        }
        else
        {
          document.write("Putting off 'todo' #" + j + " until later<br>")
          numbers.push(j)
        }
      }
    
      document.write("<br>Finished processing the priority tasks.")
      document.write("<br>Commencing stored tasks, most recent first.<br><br>")
    
      document.write("Now processing 'todo' #" + numbers.pop() + "<br>")
      document.write("Now processing 'todo' #" + numbers.pop() + "<br>")
      document.write("Now processing 'todo' #" + numbers.pop() + "<br>")
    </script>
    <script>
      sports = ["Football", "Tennis", "Baseball", "Hockey"]
      sports.reverse()
      document.write(sports)
    </script>
    <script>
      // Alphabetical sort
      sports = ["Football", "Tennis", "Baseball", "Hockey"]
      sports.sort()
      document.write(sports + "<br>")
    
      // Reverse alphabetical sort
      sports = ["Football", "Tennis", "Baseball", "Hockey"]
      sports.sort().reverse()
      document.write(sports + "<br>")
    
      // Ascending numeric sort
      numbers = [7, 23, 6, 74]
      numbers.sort(function(a,b){return a - b})
      document.write(numbers + "<br>")
    
      // Descending numeric sort
      numbers = [7, 23, 6, 74]
      numbers.sort(function(a,b){return b - a})
      document.write(numbers + "<br>")
    </script>
    <script>
      displayItems("Dog", "Cat", "Pony", "Hamster", "Tortoise")
    
      function displayItems(v1, v2, v3, v4, v5)
      {
        document.write(v1 + "<br>")
        document.write(v2 + "<br>")
        document.write(v3 + "<br>")
        document.write(v4 + "<br>")
        document.write(v5 + "<br>")
      }
    </script>
    <script>
      function displayItems()
      {
        for (j = 0 ; j < displayItems.arguments.length ; ++j)
          document.write(displayItems.arguments[j] + "<br>")
      }
    </script>
    <script>
      document.write(fixNames("the", "DALLAS", "CowBoys"))
    
      function fixNames()
      {
        var s = ""
    
        for (j = 0 ; j < fixNames.arguments.length ; ++j)
          s += fixNames.arguments[j].charAt(0).toUpperCase() +
               fixNames.arguments[j].substr(1).toLowerCase() + " "
    
        return s.substr(0, s.length-1)
      }
    </script>
    <script>
      words = fixNames("the", "DALLAS", "CowBoys")
    
      for (j = 0 ; j < words.length ; ++j)
        document.write(words[j] + "<br>")
        
      function fixNames()
      {
        var s = new Array()
    
        for (j = 0 ; j < fixNames.arguments.length ; ++j)
          s[j] = fixNames.arguments[j].charAt(0).toUpperCase() +
                 fixNames.arguments[j].substr(1).toLowerCase()
    
        return s
      }
    </script>
    <script>
      function User(forename, username, password)
      {
        this.forename = forename
        this.username = username
        this.password = password
    
        this.showUser = function()
        {
          document.write("Forename: " + this.forename + "<br>")
          document.write("Username: " + this.username + "<br>")
          document.write("Password: " + this.password + "<br>")
        }
      }
    </script>
  • 相关阅读:
    轻重搭配
    EF的优缺点
    使用bootstrap-select有时显示“Nothing selected”
    IIS发布 HTTP 错误 500.21
    js添加的元素无法触发click事件
    sql server查看表是否死锁
    sql server把一个库表的某个字段更新到另一张表的相同字段
    SQLSERVER排查CPU占用高的情况
    SQL server中如何按照某一字段中的分割符将记录拆成多条
    LINQ to Entities does not recognize the method 'System.DateTime AddDays(Double)' method, and this method cannot be translated into a store expression.
  • 原文地址:https://www.cnblogs.com/tszr/p/12382863.html
Copyright © 2011-2022 走看看