创建对象
// 方法1
var obj = {
name : '';
}
// 方法2
var obj1 = new Object();
var test = new Object({
name: 'test',
})
访问对象内的属性值
obj.name;
obj['name'];
如果访问对象中不存在的一个属性 则会返回undefined
往对象中添加键值
obj.name = 'jack';
使用构造函数创建对象
function test(name, age) {
this.name = name
this.age = age
}
var obj = new test('jack', 18)
console.log(obj.name);
遍历对象属性 值
var obj = {
name: 'haoran',
age: 18,
sex: '男'
}
for (var a in obj) { // 遍历
console.log(a); // 打印属性名
console.log(obj[a]);
}
// 判断对象中是否含有此属性 true/false
console.log('age' in obj); // true
console.log('height' in obj); // false
在对象中封装函数
var myMath = {
PI: 3.14,
max: function() { // 筛选出数组中最大一个数
var max = arguments[0]
for (let i = 1; i < arguments.length; i++) {
if (arguments[i] > max) {
max = arguments[i]
}
}
return max
}
}
console.log(myMath.PI);
console.log(myMath.max(10, 50, 65, 214));
常用Math对象
console.log(Math.PI); // 圆周率
console.log(Math.abs('-33')); // 求绝对值
console.log(Math.max(243, 54, 76)); // 求最大值
console.log(Math.min(54, 65, 23, 7, 23)); // 求最小值
console.log(Math.pow(4, 8)); // 获取 次幂
console.log(Math.sqrt(9)); // 获取平方根
console.log(Math.ceil(3.6666)); // 向上取整
console.log(Math.floor(3.9999)); //向下取整
console.log(Math.round(3.4)); // 获取四舍五入后的整数
console.log(Math.random()); // 获取大于0小于1的随机数
生成指定范围的随机数
// 表示生成大于或等于min且小于max的随机值
Math.random()*(max-min)+min;
// 表示生成0到任意数之间的随机整数
Math.floor(Math.random()*(max + 1));
// 表示生成1到任意数之间的随机整数
Math.floor(Math.random()*(max+1)+1);
函数封装随机数
<script>
function getRandom(max, min) {
return Math.floor(Math.random() * (max - min + 1) + min);
}
var arr = ['aa', 'bb', 'cc', 'dd']
console.log(arr[getRandom(arr.length - 1, 0)]);
</script>
猜数字游戏
<script>
function getRandom(max, min) {
return Math.floor(Math.random() * (max - min + 1) + min);
}
var random = getRandom(10, 0); // 控制数值范围
while (true) {
var num = prompt('输入你猜的数(1~10)')
if (num > random) {
alert('猜大了哦')
} else if (num < random) {
alert('猜小了哦')
} else {
alert('恭喜你 猜对了!');
break;
}
}
</script>
日期对象Date的使用
var date = new Date();
console.log(date.getFullYear()); // 年
var month = date.getMonth() + 1;
console.log(month); // 月
console.log(date.getDate()); // 日
console.log(date.getDay()); // 星期
console.log(date.getHours()); // 时
console.log(date.getMinutes()); // 分
console.log(date.getSeconds()); // 秒
console.log(date.getTime()); // 获取从1970-01-01 00:00:00距离Date对象所代表时间的毫秒数 时间戳
小案例(如果时间值为单个数是 为了美观前面加上0)
function getTime() {
var time = new Date()
var h = time.getHours();
h = h < 10 ? '0' + h : h;
var m = time.getMinutes();
m = m < 10 ? '0' + m : m;
var s = time.getSeconds();
s = s < 10 ? '0' + s : s;
return h + ':' + m + ':' + s;
}
console.log(getTime());
获取时间戳
// 获取时间戳
var date = new Date();
// 方法一
console.log(date.valueOf());
// 方法二
console.log(date.getTime());
// 方法三
var date2 = +new Date();
console.log(date2);
// 方法四
console.log(Date.now());
用时间戳算出代码执行的时间
var time1 = +new Date();
for (let i = 1, str = ''; i < 90000; i++) {
str += i;
}
var time2 = +new Date();
console.log('代码运行时间是' + (time2 - time1) + '毫秒');
获取距离 输入时间 的时间
function count(time) {
var nowtime = +new Date()
// 获取时间戳
var inputtime = +new Date(time)
// 获取用户输入时间的时间戳
var times = (inputtime - nowtime) / 1000 // 总秒数
var d = parseInt(times / 60 / 60 / 24) // 天数
d = d < 10 ? '0' + d : d
var h = parseInt(times / 60 / 60 % 24) // 小时
h = h < 10 ? '0' + h : h
var m = parseInt(times / 60 % 60) // 分钟
m = m < 10 ? '0' + m : m
var s = parseInt(times % 60) // 秒
s = s < 10 ? '0' + s : s
return d + '天' + h + '时' + m + '分' + s + '秒'
}
console.log(count('2021-3-31 11:50:00')); //输入时间
判断是否为数组 instanceof
var arr = [],
obj = {};
// 方法一
console.log(arr instanceof Array); // true
console.log(obj instanceof Array); // false
// 方法二
console.log(Array.isArray(arr)); // true
console.log(Array.isArray(obj)); // false
数组增删 push() unshift() pop() shift()
push()
var arr = [1, 2, 3, 4, 5, 6]
arr.push(8) // 在数组末尾添加数据
console.log(arr); // arr = [1, 2, 3, 4, 5, 6, 8]
console.log(arr.push()); // 返回数组长度
unshift()
var arr = [1, 2, 3, 4, 5, 6]
arr.unshift(0) // 在数组头部添加数据
console.log(arr);
console.log(arr.unshift()); // 返回数组长度
pop()
var arr = [1, 2, 3, 4, 5, 6]
arr.pop(); // 删除数组最后一个值
console.log(arr);
console.log(arr.pop()); // 返回被删除的那个值
shift()
var arr = [1, 2, 3, 4, 5, 6]
arr.shift();
console.log(arr); // 删除数组第一个值
console.log(arr.shift()); // 返回被删除的那个值
小案例:把数组中符合条件的数值放入新数组
var arr = [100, 1342, 645, 6555, 3434, 7653, 123]
var newArr = []
for (let i = 0; i < arr.length - 1; i++) {
if (arr[i] > 1000) {
newArr.push(arr[i])
}
}
console.log(newArr);
反转数组 reverse()
var arr = [1, 2, 3, 4, 5, 6, 7, 8, 9]
arr.reverse(arr)
console.log(arr); // arr = [9,8,7,6,5,4,3,2,1]
数组排序
var arr = [12, 4, 65, 3, 8, 23]
arr.sort(function(a, b) {
return a - b // a-b是升序 b-a是降序
})
console.log(arr);
数组索引 indexOf() lastIndexOf()
var arr1 = ['a', 'b', 'c', 'd', 'e']
//indexOf()是从数组前面开始检索
//lastIndexOf()是从数组后面开始检索
console.log(arr1.indexOf('e')); // 返回下标4
console.log(arr1.indexOf('t')); // 如果检索不到 返回-1
console.log(arr1.lastIndexOf('e')); // 返回下标4
案例
function unique(arr) {
var newArr = []
for (let i = 0; i < arr.length; i++) {
if (newArr.indexOf(arr[i]) === -1) {
newArr.push(arr[i])
}
}
return newArr;
}
var test = unique(['a', 'b', 'c', 'c', 'a', 'b']);
console.log(test);
数组转换为字符串
// 数组转换为字符串
var arr = ['a', 'b', 'c', 'd']
console.log(arr.toString());
console.log(arr.join());
console.log(arr.join('-')); // 指定连接符号
console.log(arr.join('\\'));
splice()
var arr = ['a', 'b', 'c', 'd', 'e', 'f']
console.log(arr.splice(2, 2)); // 从下标2开始删除两个数值 删除c,d并返回一个新的数组
console.log(arr); // 旧数组中没有c,d
slice()
var arr1 = ['a', 'b', 'c', 'd', 'e', 'f']
console.log(arr1.slice(4, 6)); //提取数组中的值 从下标4开始 提取到下标6 不包括6
console.log(arr1); // 原数组不受破坏
concat()
var arr = [1, 2, 3, 4, 5]
var arr1 = [22, 33]
console.log(arr.concat(arr1)); //连接两个数组
判断字符串中一个值出现多少次
var str = 'hello world,hello jaascript';
var index = str.indexOf('o');
var num = 0;
while (index != -1) {
console.log(index);
index = str.indexOf('o', index + 1);
num++;
}
console.log('o出现的次数是' + num);