可以理解为一种无序的数据集合
比如 描述学生信息
对象由属性和方法组成
let 对象名 = {
属性名:属性值,
方法名:函数
}信息或叫特征(名词) 例如:
let person = {
uname: 'Tricia',
age: 18,
gender: '女'
}属性访问 可以使用.或者[]获得对象 例如: 1.
let person = {
uname: 'Tricia',
age: 18,
gender: '女'
}
console.log(person.uname)
console.log(person.age)
console.log(person.gender)let person = {
uname: 'Tricia',
age: 18,
gender: '女'
}
console.log(person['uname'])
console.log(person['age'])
console.log(person['gender'])功能或叫行为(动词) 例如:
let person = {
name: 'Tricia'
sayHi: function() {
document.write('Hi~~~~~')
}
}方法访问: 对象名.方法名() 注意:一定不要忘记()
person.sayHi()操作数据就是增 删 改 查
对象名.新属性名 = 新值
let goods = {
name: '小米10 青春版',
num: 1000011026024,
weight: '0.55kg',
address: '中国大陆'
}
goods.price = 1099对象名.方法名 = function() { 方法 }
let goods = {
name: '小米10 青春版',
num: 1000011026024,
weight: '0.55kg',
address: '中国大陆'
}
goods.useMethod = function() {
alert('欢迎使用')
}delete 对象名.属性名
对象.属性 = 值 对象.方法 = function() {}
对象.属性 (或者 对象[‘属性’]) 对象.方法()
语法:
for in
let obj = {
uname : 'Tricia',
age : 18,
gender : '女'
}
for (let k in obj) {
// k 是获得对象的属性名
console.log(k) // 打印属性名
console.log(obj[k]) // 打印属性值
// 对象名[k] 是获得属性值
}JavaScript内部提供的对象,包含各种属性和方法给开发者调用
提供了一系列数学运算方法。
Math.floor(Math.random() * (10 + 1))
2. 生成5-10的随机数
Math.floor(Math.random() * (5 + 1)) + 5
3. 生成N-M之间的随机数
Math.floor(Math.random() * (M - N + 1)) + N
console.log(Math.ceil(0.95));
// expected output: 1
console.log(Math.ceil(4));
// expected output: 4
console.log(Math.ceil(7.004));
// expected output: 8
console.log(Math.ceil(-7.004));
// expected output: -7Math.floor( 45.95);
// 45
Math.floor( 45.05);
// 45
Math.floor( 4 );
// 4
Math.floor(-45.05);
// -46
Math.floor(-45.95);
// -46Math.max(value1[,value2, ...])
console.log(Math.max(1, 3, 2));
// expected output: 3
console.log(Math.max(-1, -3, -2));
// expected output: -1
const array1 = [1, 3, 2];
console.log(Math.max(...array1));
// expected output: 3Math.min([value1[,value2, ...]])
Math.abs('-1'); // 1
Math.abs(-2); // 2
Math.abs(null); // 0
Math.abs("string"); // NaN
Math.abs(); // NaN