• 吴裕雄--天生自然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>
  • 相关阅读:
    java核心学习笔记(三) java集合框架
    java核心学习笔记(二) 学习环境与学习的方法以及java几个包的作用
    java核心学习笔记(一) javaJDK目录阐述
    版本控制工具git的配置
    linux系统管理员之自动化检测工具 nagios及其插件配置
    运维工程师需要的技能
    在线求中位数(优先队列实现) POJ3784
    并查集 poj1308
    stack HDU1022
    hash数组 POJ1840
  • 原文地址:https://www.cnblogs.com/tszr/p/12382863.html
Copyright © 2020-2023  润新知