js内置对象

js内置对象

一、日期对象

Date Math(数学运算)

1、Date 日期对象

var s1=new Date() 
    d1.getDate()  日
    d1.getDay()   星期  1 2 3 4 5 6 0-星期日
    d1.getHours() 小时 1.。。23 0
    d1.getMinutes() 分钟 1.。。59 0
    d1.getMonth()0.。。11
    d1.getSeconds()1.59..0
    d1.getTime()   返回1970.1.1午夜(零时)开始到当前时间的“毫秒”数
    d1.getFullYear()

2.格式化日期对象

var s1=new Date() 
    console.log(s1.toLocaleDateString())  //2022/7/11
    console.log(s1.toLocaleString())   //2022/7/11 23:22:43
    console.log(s1.toLocaleTimeString())  //23:22:43

3.----原型封装

对象 Date.prototype

Date.prototype.formatDate=function(type){
            var t=new Date()
            var a=t.getFullYear()
            var b=t.getMonth()+1
            var c=t.getDay()
            switch(type){
                case "-":return a+"-"+b+"-"+c;break;
                case "/":return a+"/"+b+"/"+c;break;
                case ".":return a+"."+b+"."+c;break;
                default: return a+"年"+b+"月"+c+"日"
            }
        }

        var d2= new Date()
        var ndate=d2.formatDate("-")
        alert(ndate)

4.日期格式化—升级版

formatdate("yyyy-MM-dd hh:mm:ss")//年月日,时分秒
formatdate("yyyy-MM-dd")

二、js内置对象–Math

    abs()———— 绝对值
    sqrt(4)————2  开平方
    pow(2,3)————2^3    返回XY次幂的值

1.浮点数测

(1)floor()————向下取整
(2)round()————四舍五入
	23.67——23.7 round
	console.log(Math.round(23.67*10)/10);
(3)v1.toFixed()————截取位数,四舍五入【截取小数】
(4)ceil() ————向上取整
    Math.ceil(23.6)  24
(5)sin(30)三角函数 -1————1之间

2. random 随机数

  random  随机数【每刷新一次都会出现一个新的数】
  Math.random()   返回0-1之间的随机浮点数——不包含01
(1)返回1-10之间的随机整数
 a=Math.random()*10+1
    console.log(Math.floor(a))
(2)返回0-10之间的随机整数
 Math.round(Math.random() * 10)      //0-9
    Math.floor(Math.random()*11)        //0-10【0.99*11=10.89】
(3)返回指定范围的随机整数
   f1(10,200) 包含10200
    【1】f1(起始,终止)  包含起始和终止
    console.log(Math.floor(Math.random()*(20-10+1))+10)
    Math.floor(Math.random() * (max - min + 1)) + min;

    【2】f1(起始,终止)  包含起始和不包含终止
    console.log(Math.floor(Math.random()*(20-10))+10)

    【3】f1(起始,终止)  不包含起始和不包含终止
    console.log(Math.ceil(Math.random()*(20-1-10))+10)

//生成随机数,随机数去重,上下范围0.1

 function random_fun(rdata,min,max) {
        var r_number = (Math.random() * (max - min) + min).toFixed(1)
        if (rdata.length > 0) {
            if (rdata.indexOf(r_number) > -1) {
                return random_fun(rdata, min, max)
            }
        }
    }

三、数组

1.构建数组
数组定义:数组是保存任意类型的数据结合
    (1)构造函数方式实现
        var a1=new Array()  '空'数组
    (2)直接量实现
    var a2=[]
2.数组数据操作
(1)a1[0]赋值
    var a1=[]
    a1[0]="hello"
    console.log(a1)

    var a1=["q","b","c"]
    console.log(a1[1])      //b
2)长度
var a1=new Array(3)  指定长度(3)
console.log(a1[0]) //undefined【相当于申明未赋值】

    var a1=new Array(3) 
    console.log(a1[0]) //undefined
    console.log(a1.length)//3

    var a2=[,,,,]   //最后一个逗号忽略,所以按4来提
    console.log(a2[2])  //undefined
    console.log(a2.length)//4 
3.数组的遍历【数组下标由0开始】
var a1=[23,35,34,13]
 (1)for(var i=0;i<a1.length;i++){
    console.log(a1[i])
   }
(2)for in
    for(var i in a1){
        console.log(a1[i])
    }
4.数组去重
var arr1=[23,45,23,56,45,89]    去重    arr1=[23,45,56,89]
      //生成[1..36]数组,随机选择5个数,不重复
        // 如:12 23 3 7 29
        // var a=[];
        // var t=0
        // for(var i=0;i<=35;i++){
        //     a[i]=i+1;
        // }
        // var n=[]
        // while(t!=5){
        //     var num=parseInt(Math.random()*36)
        //     if(n.indexOf(a[num])==-1){
        //         n[t]=a[num]
        //         t++
        //     }
        // }
        // console.log(n)
5.数组的添加与删除—[push() unshift()]
  • 尾部增加:push() 数组尾部增加一个或多个数据,对原数组进行修改,返回修改后的数组长度 。
  • 尾部删除:pop() 删除数组尾部的一个元素,对原数组进行修改,返回的是被删除的元素
  • 头部增加:unshift( ) 数组的头部增加元素,对原数组进行修改,返回修改后的数组长度
  • 头部删除:shift() 删除数组的头部元素,对原数组进行修改,返回被删除元素
var a=[12,32,19,4,23]
    a.push(54)
    console.log(a)  //[ 12, 32, 19, 4, 23, 54 ]
    b=a.push(54)
    console.log(b)      //6
var a=[15,32,12,4,42]
	a.pop()
	console.log(a) //[ 15, 32, 12, 4 ]
 var a=[15,32,12,4,42]
    // a.unshift(23)
    // console.log(a) //[ 23, 15, 32, 12, 4, 42 ]
    console.log(a.unshift(23))  //6
 var a=[15,32,12,4,42]
     a.shift()
     console.log(a) // [ 32, 12, 4, 42 ]
     console.log(a.shift()) //32
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值