5.7 built-in objects monomer -Global, Math

Global Objects

He explained: "reveal the object." It does not belong to any of the properties and methods of other objects, and ultimately its properties and methods. In fact there is no global variables and functions are properties of global object. Including isNaN (), isFinite (), parseInt (), parseFloat (), as well as methods

1.URI coding method

encodeURI()

encodeURIComponent()

Use encodeURIComponent () method more than encodeURI ().

栗子:var uri = "http://www.wrox.com/illegal value.htm#start"; 

           //"http://www.wrox.com/illegal%20value.htm#start " 
           of nco shed (on)

          //"http%3A%2F%2Fwww.wrox.com%2Fillegal%20value.htm%23start" 
           encodeURIComponent(uri)

2.eval () method

  Only accepts one parameter, a string that is to be executed. It will be passed as actual parameters js statement to resolve.

  Code is executed may contain references to variables defined in the environment.

  You can define a function eval (), and then reference the function in the function of the external code.

  In strict mode, not visit eavl (any variable or function) created externally.

Chestnut: eval ( "alert ( 'hi')") is equal to alert ( "hi") 

          var mes = "hello world"

          eval("alert(msg)")    //"hello world"

         eval("function sayHI(){alert('hi')}")

         sayHI ()

3.window objects

  In all variables and functions at global declaration space, it has become the property of the window object.

  Chestnuts: var color = "red"

            function saycolor(){

                  alert(window.color)     //"red"
            }

Math object

1.min () and max () method

   Means for determining a set of minimum and maximum values ​​in the array. Any number of parameters.

  Chestnut: var max = Math.max (3,5,32,16) var min = Math.max (3,5,32,16)

            alert (max) 32 // alert (min) // 3

  To find an array maximum or minimum value, you can use the apply () method

   Chestnut: var values ​​= [1, 2, 3, 4, 5, 6, 7, 8]

                var max = Math.max.apply (this, values) // this value of the window, so this can also be written window, Math. In practice this is to change the point of

2. Rounding Method

   Math.ceil () rounds up

  Math.floor () rounded down

  Math.round () rounding

3.random () method of the random number

   Math.random () method returns a random number greater than 0 to less than 1.

  Chestnut: randomly selecting a value within a range of integers.

             Value = Math.floor (Math.random () * + total number of possible values ​​of a possible value)

             Such as: 1 to 10 random number directly

                     var n = Math.floor(Math.random() * 10 + 1)

                     A random number between 2-10

                     var m = Math.floor( Math.random() * 9 +2 )

Chestnut: a randomly taken from the array

          function select (lower, upper) { // accepts two parameters: minimum and maximum values should return
                var chioce = upper - lower +1

                return Math.floor( Math.random() * chioce + lower)
          }

          var num = select (2.10) alert (number);

          var colors = ["red", "green", "blue", "yellow", "black", "purple", "brown"]; 
          var color = colors[selectFrom(0, colors.length-1)]; 
          alert(color);

           

Published 54 original articles · won praise 8 · views 70000 +

Guess you like

Origin blog.csdn.net/yang295242361/article/details/90170186