• 单体内置对象


    内置对象:由ECMAscript实现提供的,不依赖宿主环境的对象,这些对象再ES程序执行之前就已经存在

    Global对象

    Global全局对象是一个特别的对象(兜底对象)
    不属于其他对象的属性和方法,都属于Global对象。
    如:isNaN()① isFinite()② parseInt() parseFloat()

    注解:
    ① isNaN 方法用于检查其参数是否非数值,通常用于检测parseInt()和parseFloat()的结果。(如果是非数值则返回true,数值则返回false)
    ② isFinite() 方法用于检测其参数是否是有限数

    URI编码方法

    Global对象中有两个方法可以对URI编码,以方便发给浏览器:

    • encodeURI() 可以对整个URI进行编码,因为它不会对属于URI的特殊字符进行编码,比如:斜杠/冒号等;
    • encodeURIComponent() 主要适用于对URI中的某一段进行编码,因为它会对任何非标准字符进行编码。
    var uri = 'http://www.baidu.com/tieba/index.html#serch 134';
    
    console.log('encodeURI: ' + encodeURI(uri));
    console.log('encodeURIComponent: ' + encodeURIComponent(uri));
    
    /* 输出
    encodeURI: http://www.baidu.com/tieba/index.html#serch%20134
    encodeURIComponent: http%3A%2F%2Fwww.baidu.com%2Ftieba%2Findex.html%23serch%20134
    */
    

    相对应的,它们有编码,自然也会有解码:

    • decodeURI()
    • decodeURIComponent()
    var uri = 'http://www.baidu.com/tieba/index.html#serch 134';
    
    var codeuri1 = encodeURI(uri);
    var codeuri2 = encodeURIComponent(uri);
    
    console.log('uri1解码前: ' + codeuri1);
    console.log('uri1解码后:' + decodeURI(codeuri1));
    
    console.log('uri2解码前: ' + codeuri2);
    console.log('uri2解码后:' + decodeURIComponent(codeuri2));
    
    
    /* 输出
    uri1解码前: http://www.baidu.com/tieba/index.html#serch%20134
    uri1解码后:http://www.baidu.com/tieba/index.html#serch 134
    uri2解码前: http%3A%2F%2Fwww.baidu.com%2Ftieba%2Findex.html%23serch%20134
    uri2解码后:http://www.baidu.com/tieba/index.html#serch 134
    */
    
    eval()方法

    eval()类似一个完整的es解析器。

    当解析器中发现调用eval()的时候,会将传入的参数当作实际的ES语句来解析,然后把执行结果插入到原位置。

    eval("function sayRe(){console.log('我的天,热炸了')}");
    sayRe();
    
    /* 输出 
    我的天,热炸了
    */
    

    Math对象

    Math对象是ES为数学公式及信息提供的一个公共位置。
    Math对象包含了许多方法,用于辅助完成简单和复杂的数学计算,常用方法:

    min()max()

    确认一组数值中的最大值和最小值,两个方法都可以任意多个数值参数。

    min()max()可以在一组数值中找到最大值和最小值,但不能直接在数组上应用,直接用在数组上会返回NAN.

    var arry = [23,13,2,53,6,12,4];
    
    console.log('arry中最小: ' + Math.min(arry));
    console.log('arry中最大: ' + Math.max(arry));
    
    console.log(Math.min(23,13,2,53,6,12,4));
    console.log(Math.max(23,13,2,53,6,12,4));
    
    
    /* 输出 
    arry中最小: NaN
    arry中最大: NaN
    2
    53
    */
    

    如果要找到数组中的最大最小,需要利用applay方法。

    var arry = [23,13,2,53,6,12,4];
    
    var min = Math.min.apply(Math, arry);
    var max = Math.max.apply(Math, arry);
    
    console.log(`min = ${min}, max = ${max}`);
    
    
    /* 输出
    min = 2, max = 53
    */
    
    这里就需要来复习一下apply()方法的用途了:
    每个函数都包含两个非继承来的方法:apply() 和call().
    apply()有两个用途,一个是扩大作用域,第二个就是传参了。
    它有一个特性是可以将接收到的数组,转换为一个参数列表形式。

    参考资料:https://www.cnblogs.com/chenhuichao/p/8493095.html

    所以,在math.min()不支持直接传递数组形式的时候,就可以利用apply()方法可以将数组转化为参数列表的特性,来完成数组找到最大最小值。

  • 相关阅读:
    Mac下Intellij IDEA Console中文是?
    MAC && Linux terminal session clone
    高扩展性网站的原则
    职场感悟
    如何保持自己 fork 的项目和原始项目同步
    统一回复一下同学们的咨询
    Can't exec "aclocal": No such file or directory at /usr/share/autoconf/Autom4te/FileUtils.pm line 326.
    checking for tgetent()... configure: error: NOT FOUND!
    Simultaneous Tag Editing in IntelliJ IDEA 14.1
    Mac 配置 vim
  • 原文地址:https://www.cnblogs.com/mlcat/p/11503964.html
Copyright © 2020-2023  润新知