[Web front-end] 025 js object, and an array of mathematical objects

1. Javascript Object

1.1 to create objects

1.1.1 Use original way to create a built-in objects

var myObject = new Object();
myObject.name = "lijie";
myObject.age = 20;
myObject.say = function(){...}

1.1.2 directly create custom objects

var 对象名 = {属性名1: 属性值, 属性名2: 属性值2, ...}

1.1.3 Creating an object using a custom constructor

function pen(name, color, price){
    // 1. 对象的 name 属性
    this.name = name;
    
    // 2. 对象的 color 属性
    this.color = color;
    
    // 3. 对象的 piece 属性
    this.price = price;
    
    // 4. 对象的 say 方法
    this.say = function(){};
}

var pen = new pen("铅笔", "红色", 20);
pen.say();

1.2 this keyword

  • this The intention is the word这个
  • When the object call a method, this method represents the object
  • Similar Python inself

1.3 traversal

  • for-in statement can iterate over all the elements or all attributes of an array of objects
for(var i in window){
    document.write(i + "---" + window[i]);
}

About 1.4 type

  • Test Type
// 1. global 对象的其中一个方法
typeof()

// 2. 查看当前对象的构造函数是谁
对象.constructor;

// 3. 数组推荐用这种方法,因为 typeof() 得到是 object
if(arr.constructor == Array){
    alert("数组");
}

2. Javascript Array

  • Array is a collection of data
  • JS , arrays which data may be different types of

The method defined array 2.1

// 1. 用对象的实例创建
var aList1 = new Array(1, 2, 3);

// 2. 直接量创建
var aList2 = [1, 2, 3, "abc"];

2.2 Method of operation data in the array

2.2.1 acquired length of the array

  • aList.length;
var aList = [1, 2, 3, 4];
alert(aList.length); // alert 4

2.2.2 with a data array subscripting

  • aList[0];
var aList = [1, 2, 3, 4];
alert(aList[0]); // alert 1

2.2.3 At the end of the array deletions members

  • push () and pop ()
var aList = [1, 2, 3, 4];
aList.push(5);
alert(aList); // alert 1, 2, 3, 4, 5
aList.pop();
alert(aList); // alert 1, 2, 3, 4

2.2.4 additions and deletions in the head array members

  • unshift()shift()
var aList = [1, 2, 3, 4];
aList.unshift(5);
alert(aList); // alert 5, 1, 2, 3, 4
aList.shift();
alert(aList); // alert 1, 2, 3, 4

2.2.5 to add or remove members in the array

  • splice()
var aList = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12];

// 1. 若 splice() 只接受 1 个参数,表示删除该下标至末尾的所有元素,包括该下标
aList.splice(11);
console.log("1. aList =", aList);

// 2. 若 splice() 只接受 2 个参数,表示删除该两个下标间的所有元素,包括该下标
aList.splice(1, 3);
console.log("2. aList =", aList);

// 3. 若 splice() 只接受 3 个参数,前 2 个意思同上,第 3 个元素用来填补删除处
aList.splice(1, 3, 100);
console.log("3. aList =", aList);

// 4. 若 splice() 接受 3 个以上参数,前 3 个意思同上,第 4 个及之后的元素也用来填补删除处
aList.splice(1, 3, 101, 102, 103, 104);
console.log("4. aList =", aList);
  • operation result
1. aList = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
2. aList = [0, 4, 5, 6, 7, 8, 9, 10]
3. aList = [0, 100, 7, 8, 9, 10]
4. aList = [0, 101, 102, 103, 104, 9, 10]

2.3 Multidimensional Arrays

  • Multidimensional array refers to an array is an array of members of the array
var aList = [[1, 2, 3], ['a', 'b', 'c']];
alert(aList[0][1]); // alert 2

3. Javascript mathematical objects

3.1 Math

// 1. 四舍五入
var res1 =  Math.round(5.921);
var res2 =  Math.round(5.321);
console.log("res1 =", res1);
console.log("res2 =", res2);


// 2.1 获取最大值
var res3 =  Math.max(10, 20, 30, 41, 52, 14, 28, 39);
console.log("res3 =", res3);

// 2.2 获取最小值
var res4 =  Math.min(10, 20, 30, 41, 52, 14, 28, 39);
console.log("res4 =", res4);


// 3. 获取绝对值
var res5 = Math.abs(-100);
console.log("res5 =", res5);


// 4.1 退一取整(向下取整)
var res6 = Math.floor(1.1);
var res7 = Math.floor(1.9);
console.log("res6 =", res6);
console.log("res7 =", res7);

// 4.2 进一取整(向上取整)
var res8 = Math.ceil(1.1);
var res9 = Math.ceil(1.9);
console.log("res8 =", res8);
console.log("res9 =", res9);


// 5. 幂运算
var res10 = Math.pow(2, 3);
console.log("res10 =", res10);


// 6. 开方运算
var res11 = Math.sqrt(9);
console.log("res11 =", res11);
  • operation result
res1 = 6
res2 = 5
res3 = 52
res4 = 10
res5 = 100
res6 = 1
res7 = 1
res8 = 2
res9 = 2
res10 = 8
res11 = 3

3.2 random()

  • Obtaining a random number, returns a random decimal between (0-1]
  • From the perspective of probability theory, probability, may take to the 1, but was taken to 0
// 手动封装函数
function rand(m, n){
    return Math.floor(Math.random() * (n-m+1)) + m; // m 写到 floor() 中也行
}
var res = rand(20, 30);

Guess you like

Origin www.cnblogs.com/yorkyu/p/11305824.html