Js built-in objects Math Primer

First, review the data type

Simple data types, basic data type / value type
     Number String Boolean Null Undefined
Complex data types reference types
    Object Array

Data in memory is how to store
Heap: complex type deposit
Stack: simple type deposit

Simple internal modification type function does not affect the value of a simple type external data is stored directly on the stack
It will affect the complex type because there are complex refer to the same type of a memory heap area at the same time will generate a memory address on the stack points to the heap  


Built-in objects (MDN to view the document)
js objects custom object built-in objects browser object (not part of ECMAScript)

Math/Array/Date

Mozilla Developer Network (MDN) provides information on open web technologies including HTML CSS and Web
And API HTML5 applications

https://developer.mozilla.org/zh-CN/docs/Learn
View Code

Two, Math Object

Math object is not a constructor, which method has the properties and functions are mathematical constants provide a mathematical operation with members come in Math (absolute value, an integer) in the manner of a static member

Math.PI pi (a property)
Math.random () generates a random number
Math.floor () rounds down
Math.ceii () rounding up
Math.round () rounding
Math.abs () absolute value
Math.max /Math.min () takes the maximum value / minimum value
    Returns a set of numbers given maximum value, if the given parameter at least one parameter can not be converted to a number, it returns NaN
Math.sin () /Math.cos () SineCosine
Math.power () / Math.sqrt () Exponential power / square root

Case 1: find a value between 10 and 20

function random(min, max) {
    return Math.floor(Math.random() * (max - min + 1) + min);
}
console.log(random(10, 20));

Case 2: analog max and min of

was MyMath = {
max: function () {
var max = arguments[0];
for (var i = 1; i < arguments.length; i++) {
    if (max < arguments[i]) {
        max = arguments[i];
    }
}
return max;
},
    min: function () {
        min = arguments[0];
        for (var i = 1; i < arguments.length; i++) {
            if (min > arguments[i]) {
                min = arguments[i];
            }
        }
            Return min;
    }
};
console.log(MyMath.max(23,21,33));
View Code

 

Guess you like

Origin www.cnblogs.com/guniang/p/11987334.html