JavaScript-内置对象

时间:2019-10-17
本文章向大家介绍JavaScript-内置对象,主要包括JavaScript-内置对象使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

内置对象

Math

JavaScript中内置的Math对象提供了与数学相关操作的API。

Math.abs():取绝对值
Math.ceil():向上取整
Math.floor():向下取整
Math.PI:圆周率
...

Date

JavaScript中内置的Date对象提供了与日期相关操作的API。

Date.now():获取当前时间
Date.getFullYear():获取当前年份
Date.getMonth():获取当前月份(由于月份是从0开始算的,所以实际月份要 +1)
Date.getDate():获取当前日期
Date.getDay():获取当前周几
Date.getHours():获取当前小时数
Date.getMinutes():获取当前分钟数
Date.getSeconds():获取当前秒数
Date.getMilliseconds():获取当前毫秒数

BOM

BOM是浏览器内置的一套操作浏览器的API(方法、属性、接口)

window

在BOM中window代表整个浏览器窗口。

navigator保存了当前浏览器相关的信息,一般用于判断用户当前的浏览器。

// 判断终端及浏览器信息
function judgeTerminalBrowser (userAgent) {
    let data = {
        terminal: undefined,
        browser: undefined
    };
    let terminal = {
        'windows nt 10'      : 'Windows 10',
        'windows nt 6.3'     : 'Windows 8.1',
        'windows nt 6.2'     : 'Windows 8',
        'windows nt 6.1'     : 'Windows 7',
        'windows nt 6.0'     : 'Windows Vista',
        'windows nt 5.2'     : 'Windows Server 2003XP x64',
        'windows nt 5.1'     : 'Windows XP',
        'windows xp'         : 'Windows XP',
        'windows nt 5.0'     : 'Windows 2000',
        'windows me'         : 'Windows ME',
        'win98'              : 'Windows 98',
        'win95'              : 'Windows 95',
        'win16'              : 'Windows 3.11',
        'macintosh|mac os x' : 'Mac OS X',
        'mac_powerpc'        : 'Mac OS 9',
        'linux'              : 'Linux',
        'ubuntu'             : 'Ubuntu',
        'phone'              : 'iPhone',
        'pod'                : 'iPod',
        'pad'                : 'iPad',
        'android'            : 'Android',
        'blackberry'         : 'BlackBerry',
        'webos'              : 'Mobile',
        'freebsd'            : 'FreeBSD',
        'sunos'              : 'Solaris'
    };

    for (let key in terminal) {
        if (new RegExp(key).test(userAgent.toLowerCase())) {
            data.terminal = terminal[key];
            break;
        }
    }

    if (userAgent.match(/MSIE\s(\d+)\..*/i)) {
        // ie 除11
        data.browser = 'ie ' + userAgent.match(/MSIE\s(\d+)\..*/i)['1'];
    } else if (userAgent.match(/FireFox\/(\d+)\..*/i)) {
        data.browser = 'firefox ' + userAgent.match(/FireFox\/(\d+)\..*/i)['1'];
    } else if (userAgent.match(/Opera[\s|\/](\d+)\..*/i)) {
        data.browser = 'opera ' + userAgent.match(/Opera[\s|\/](\d+)\..*/i)['1'];
    } else if (userAgent.match(/Chrome\/(\d+)\..*/i)) {
        data.browser = 'chrome ' + userAgent.match(/Chrome\/(\d+)\..*/i)['1'];
    } else if (userAgent.match(/Safari\/(\d+)\..*$/i)) {
        // chrome浏览器都声明了safari
        data.browser = 'safari ' + userAgent.match(/Safari\/(\d+)\..*$/i)['1'];
    } else if (userAgent.match(/rv:(\d+)\..*/i)) {
        // ie 11
        data.browser = 'ie ' + userAgent.match(/rv:(\d+)\..*/i)['1'];
    }
    return data;
}

location

location保存了浏览器地址栏信息。

// 获取当前浏览器地址栏信息
console.log(window.location.href);

// 跳转到指定地址
window.location.href = "https://www.baidu.com";

// 刷新页面(不清除缓存)
window.location.reload();

// 刷新页面并强制清除缓存
window.location.reload(true);

history

history用于操作浏览器历史信息。

// 前进一个页面
window.history.forward()

// window.history.go() 当参数是正数时是多少就前进多少页面;当参数时负数时时多少就后退多少个页面;当参数是0时便刷新页面
window.history.go(0);

// 后退一个页面
window.history.back();

screen

screen对象用来表明客户端的能力,其中包括浏览器窗口外部的显示器的信息,如像素高度和宽度等。

/*
常用属性:
    availHeight:屏幕的像素高度减去系统部件高度之后的值(只读),代表屏幕可用高度,单位为像素
    availWidth:屏幕的像素宽度减去系统部件宽度之后的值(只读),代表屏幕可用宽度,单位为像素
    height:屏幕像素的高度
    width:屏幕像素的宽度
*/

原文地址:https://www.cnblogs.com/luwenfeng/p/11694886.html