重温javascript 基础【五】

145 阅读2分钟
    //=========================对象============================
	//1 利用对象字面量创建对象
	// var obj={
	// 	name:'贾民全',
	// 	age:24,
	// 	sex:'男'
	// }
	// console.log(obj.age);
	//2 构造函数
	// function Star(name,age,sex){   //构造函数名首字母大写
	// 	this.name=name;
	// 	this.age=age;
	// 	this.sex=sex;
	// 	this.live=function(xxx){
	// 		console.log(xxx);
	// 	}
	// }
	// var jmq = new Star('贾民全',24,'男');
	// var cc = new Star('陈春',24,'女');
	// console.log(jmq.name)
	// jmq.live('我爱你')
	// console.log(cc.name)
	
	//===========遍历对象==============
	// var obj={
	// 	name:'贾民全',
	// 	age:24,
	// 	sex:'男'
	// }
	// for(var k in obj){
	// 		console.log(obj[k]);
	// }
	
	//===============内置对象===============
	//内置对象就是JS语言自带的一些对象,供开发者使用,提供了一些常用的功能
	//比如:Math/Date/Array/String等    方法太多 查文档MDN 
	//案例 封装自己数学对象
	// var myMath={
	// 	PI:3.1415926,
	// 	max:function(){
	// 		var max=arguments[0];
	// 		for(var i =1;i<arguments.length;i++){
	// 			if(arguments[i]>max){
	// 				max=arguments[i]
	// 			}
	// 		}
	// 		return max
	// 	}
	// }
	// console.log(myMath.PI);
	// console.log(myMath.max(2,5,8,3));
	
	//取整方法
	//1  向下取整
	// console.log(Math.floor(1.3));  //1
	// //2  向上取整
	// console.log(Math.ceil(1.2));   //2
	// //3  四舍五入
	// console.log(Math.round(1.4));  //1
	// console.log(Math.round(1.5));  //2
	
	//随机数方法 random()
	// console.log(Math.random());
	// function get(min,max){
	// 	return Math.floor(Math.random()*(max-min+1))+min;
	// }
	// console.log(get(1,20));
	// var arr=['贾民全','陈春','我爱你','我想你']
	// console.log(arr[get(0,arr.length-1)]);
	
	//案列 猜数字
	// function get(min,max){
	// 	return Math.floor(Math.random()*(max-min+1))+min;
	// }
	// var num = get(1,10)
	
	// 	for(var i =1;i<=5;i++){
	// 		var a=prompt('请输入1-10的数字')
	// 		if(a<num){
	// 			alert('小了')
	// 		}else if(a>num){
	// 			alert('大了')
	// 		}else{
	// 			alert('聪明')
	// 			break
	// 		}
	// 	}
	
	//日期对象 Date() 是一个构造函数 必须使用new
	// var date = new Date()
	// console.log(date);
	// var date1=new Date(2020,3,14)
	// console.log(date1);
	
	// var date2=new Date('2020-3-14 8:8:8')  //主要使用
	// console.log(date2);
	//日期格式化
	// var date = new Date()
	// // console.log(date.getMonth()+1); //月份要加1
	// // console.log(date.getDay());//周日返回的是0
	// var year = date.getFullYear();
	// var month=date.getMonth()+1;
	// var dayes=date.getDate();
	// console.log('今天是:'+year+'年'+month+'月'+dayes+'日');
	//案列 倒计时
	// function countDown(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(countDown("2020-3-31 23:00:00"));