JavaScript Basics - Day 5
Know the characteristics of object data types and be able to use array objects to render pages
- Understand what an object is and master the syntax for defining objects
- Master the use of mathematical objects
object
Object is a type of JavaScript data type. We have learned about numeric types, string types, Boolean types, and undefined before. The object data type can be understood as a collection of data. It consists of two parts: properties and methods.
grammar
Declaring a variable of type object is not fundamentally different from declaring a variable of type numeric or string.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>JavaScript 基础 - 对象语法</title>
</head>
<body>
<script>
// 声明字符串类型变量
let str = 'hello world!'
// 声明数值类型变量
let num = 199
// 声明对象类型变量,使用一对花括号
// user 便是一个对象了,目前它是一个空对象
let user = {
}
</script>
</body>
</html>
Properties and access
Data descriptive information is called attributes, such as a person's name, height, age, gender, etc., which are generally nominal.
- Attributes appear in pairs, including attribute names and values,
:
separated by English ,
Use English to separate multiple attributes- Properties are variables attached to an object
- The attribute name can use
""
or''
, which is generally omitted, unless the name encounters special symbols such as spaces, dashes, etc.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>JavaScript 基础 - 对象语法</title>
</head>
<body>
<script>
// 通过对象描述一个人的数据信息
// person 是一个对象,它包含了一个属性 name
// 属性都是成对出现的,属性名 和 值,它们之间使用英文 : 分隔
let person = {
name: '小明', // 描述人的姓名
age: 18, // 描述人的年龄
stature: 185, // 描述人的身高
gender: '男', // 描述人的性别
}
</script>
</body>
</html>
After declaring an object and adding several attributes, you can use .
or []
to obtain the value corresponding to the attribute in the object, which I call attribute access.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>JavaScript 基础 - 对象语法</title>
</head>
<body>
<script>
// 通过对象描述一个人的数据信息
// person 是一个对象,它包含了一个属性 name
// 属性都是成对出现的,属性名 和 值,它们之间使用英文 : 分隔
let person = {
name: '小明', // 描述人的姓名
age: 18, // 描述人的年龄
stature: 185, // 描述人的身高
gender: '男', // 描述人的性别
};
// 访问人的名字
console.log(person.name) // 结果为 小明
// 访问人性别
console.log(person.gender) // 结果为 男
// 访问人的身高
console.log(person['stature']) // 结果为 185
// 或者
console.log(person.stature) // 结果同为 185
</script>
</body>
</html>
Extension: You can also add attributes to objects dynamically. Dynamic addition is the same as direct definition, but the syntax is more flexible.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>JavaScript 基础 - 对象语法</title>
</head>
<body>
<script>
// 声明一个空的对象(没有任何属性)
let user = {
}
// 动态追加属性
user.name = '小明'
user['age'] = 18
// 动态添加与直接定义是一样的,只是语法上更灵活
</script>
</body>
</html>
methods and calls
Data behavioral information is called methods, such as running, singing, etc., which are generally verbs, and their essence is a function.
- A method consists of two parts: method name and function, separated by:
,
Use English to separate multiple attributes- Methods are functions attached to an object
- The method name can use
""
or''
, which is generally omitted, unless the name encounters special symbols such as spaces, dashes, etc.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>JavaScript 基础 - 对象方法</title>
</head>
<body>
<script>
// 方法是依附在对象上的函数
let person = {
name: '小红',
age: 18,
// 方法是由方法名和函数两部分构成,它们之间使用 : 分隔
singing: function () {
console.log('两只老虎,两只老虎,跑的快,跑的快...')
},
run: function () {
console.log('我跑的非常快...')
}
}
</script>
</body>
</html>
After declaring an object and adding several methods, you can use .
or []
to call functions in the object, which I call method calls.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>JavaScript 基础 - 对象方法</title>
</head>
<body>
<script>
// 方法是依附在对象上的函数
let person = {
name: '小红',
age: 18,
// 方法是由方法名和函数两部分构成,它们之间使用 : 分隔
singing: function () {
console.log('两只老虎,两只老虎,跑的快,跑的快...')
},
run: function () {
console.log('我跑的非常快...')
}
}
// 调用对象中 singing 方法
person.singing()
// 调用对象中的 run 方法
person.run()
</script>
</body>
</html>
Extension: You can also dynamically add methods to objects. Dynamic addition is the same as direct definition, but the syntax is more flexible.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>JavaScript 基础 - 对象方法</title>
</head>
<body>
<script>
// 声明一个空的对象(没有任何属性,也没有任何方法)
let user = {
}
// 动态追加属性
user.name = '小明'
user.['age'] = 18
// 动态添加方法
user.move = function () {
console.log('移动一点距离...')
}
</script>
</body>
</html>
Note: Whether it is a property or a method, if the same name appears in the same object, the later one will overwrite the previous one.
null
null is also a data type in JavaScript, and is usually only used to represent non-existent objects. When using typeof to detect the type of its type, the result is object
.
Traverse objects
let obj = {
uname: 'pink'
}
for(let k in obj) {
// k 属性名 字符串 带引号 obj.'uname' k === 'uname'
// obj[k] 属性值 obj['uname'] obj[k]
}
for in is not recommended for traversing arrays because k is a string
built-in objects
Recall that what we have used is actually a built-in object in JavaScript. There is a method called in this object console.log
, and then this method is called, that is .console
log
log
console.log()
In console
addition to objects, JavaScript also has other built-in objects
Math
Math
It is a built-in object in JavaScript, called a mathematical object. This object contains properties and many methods.
Attributes
- Math.PI, get pi
// 圆周率
console.log(Math.PI);
method
- Math.random, generates a random number between 0 and 1
// 0 ~ 1 之间的随机数, 包含 0 不包含 1
Math.random()
- Math.ceil, numbers rounded up
// 舍弃小数部分,整数部分加1
Math.ceil(3.4)
- Math.floor, numbers are rounded down
// 舍弃小数部分,整数部分不变
Math.floor(4.68)
- Math.round, rounding
// 取整,四舍五入原则
Math.round(5.46539)
Math.round(4.849)
- Math.max, find the largest number in a set of numbers
// 找出最大值
Math.max(10, 21, 7, 24, 13)
- Math.min, find the smallest number in a set of numbers
// 找出最小值
Math.min(24, 18, 6, 19, 21)
- Math.pow, power method
// 求某个数的多少次方
Math.pow(4, 2) // 求 4 的 2 次方
Math.pow(2, 3) // 求 2 的 3 次方
- Math.sqrt, square root
// 求某数的平方根
Math.sqrt(16)
Mathematical objects provide a lot of methods. There is no need to memorize them here. By demonstrating the use of mathematical objects, you can deepen your understanding of the objects.