table of Contents
Object
-
Basic type
number string boolean null undefined -
Reference type
Array function object
Definition: Object refers to a specific thing. Everything is an object.
A specific thing generally has behaviors and characteristics.
-
For example:
mobile phone:
behavior: call and chat on WeChat, play peace elite and brush news.
Features: color, screen resolution, camera pixel -
Object behavior------js object function
-
The characteristics of the object-the attributes of the js object
1. Objects in JavaScript
-
Objects in JavaScript can be seen as abstractions of concrete things in life.
-
The js object is a collection of attributes + functions.
-
Attribute: It can be a basic data type or a reference data type.
2. Two ways to create an object
1. Use the new keyword class to create
2. Use literals
2.1 New attributes
1. Object name. Attribute name = attribute value;
eg: hero.a = "Han Xin";
2、eg:hero[“b”] = 30;
2.2 Use attributes
Format: Object name. Property name
2.3 New method
// 新建一个对象
var hero = new Object();
// // // 新增属性
hero.a = "韩信";
// hero.b = "30";
hero["b"] = 30;
// // 使用属性
document.write(hero.a);
// alert(hero.b);
document.write(hero["b"]);
// // 新增方法
hero.c = function() {
document.write("对方对你使用了一技能")
}
hero.c()
3 Traverse properties
For…in syntax
key represents the attribute name.
Attribute value: obj[key]
for(var key in obj){
}
4 Delete attributes
delete 对象名.属性名
Classification of objects
- 1. Built-in objects
Objects defined by the ES standard, in any ES implementation, you can use the object provided by js
Math String Number Boolean Function Object
-
2. Host Object
There are mainly objects provided by the browser.
BOM DOM -
3. Custom objects
The object defined by the developer himself.
The difference between basic types and reference types
- the difference:
The data of the basic data type is stored in the stack, and the variable directly points to the value of the basic data type.
The data of the reference data type is stored in the heap, and the variable points to the address of the reference data type.
-
Comparison:
When the reference data type is compared, the memory addresses are compared. If the memory addresses are the same and point to the same object, they are equal, otherwise they are not equal. -
Reference type
var hero = {
"a": "亚索",
"b": "狂风绝息斩"
}
var hero2 = {
"a": "亚索",
"b": "狂风绝息斩"
}
var hero2 = hero;
hero.a = "使用一技能";
document.write(hero.a);
document.write(hero2.a);
// 基础类型
var a = 10;
var b = a;
a++;
// 比较的是值,这是给a的值变了,但是b的值没变。
document.write(a);
document.write(b);
Math object
Provides a lot of calculation methods.
- Math.random(); Returns a random number between 0-1.
-
Suppose we need a number between nm.
-
Math.round(Math.random()*(m-n)+n)
-
Math.floor(Math.random() * (m - n + 1) + n);
- Math.round() rounding
- Math.max() returns the maximum value
- Math.min() returns the minimum value
- Math.abs() returns the absolute value
-
Math.ceil() round up
-
Math.floor() down value
-
Math.pow(x,y) x to the power of y
-
Math.sqrt(num) square root
-
Math.PI = 180° radians.
1 radian=Math.PI/180.
Date object
1. Create a date object.
Wed Jan 06 2021 14:38:02 GMT+0800 (China Standard Time)
Day of the week, month, day, year, hour, minute, second and time zone
creation date object is, you can specify the date. If the hour, minute, and second are not filled in, the default is 00:00:00
Several ways to specify the date:
1.new Date(“2021/01/07”)
2.new Date (2020,09,30,15,33,20);
3.new Date (1576800000000);
4.new Date(“2020-12-31 12:30:20”);
- In foreign countries, the month starts from 0, 0-11
Date object method
-
Format: Date.parse (date object);
-
Function: Convert the date object to milliseconds.
-
Get the date
d as the object name -
d.getTime() Get the current milliseconds.
-
d.getDate() return date
-
d.getDay() returns the day of the week.
-
d.getMonth() Get the month starting from 0 and the range is: 0-11
-
d.getFullYear() returns the year
-
d.getHours() Get the number of hours
-
d.getMinutes() Get the number of minutes
-
d.getSeconds() Get the number of seconds
-
d.getMilliseconds() Get the number of milliseconds
-
d.getTimezoneOffset() Gets the minute difference between local time and Greenwich Mean Time.
-
Set the date:
d is the object name -
d.setDate() Set the number of days of the date.
-
d.setMonth() set month
-
d.setFullYear() Set the year of the date
-
d.setHours() set hours
-
d.setMinutes() Set the number of minutes
-
d.setSeconds() set the number of seconds
-
d.setMilliseconds() Set the number of milliseconds