zoukankan      html  css  js  c++  java
  • 2015-03-22——js常用其它方法

    Function

    Function.prototype.method = function (name, func) {
        this.prototype[name] = func;  //此时this为Function,与Function.prototype形成了环,摆脱静态方法的局限。
        return this;
    };
    Function.method('bind', function (that) {//返回一个函数,调用这个函数,就像调用那个对象的一个方法。
        var method = this,
            slice = Array.prototype.slice,
            args = slice.apply(arguments, [1]);  //保存binds时,除了that的其它参数,以备后用。
        return function () {
            return method.apply(that, args.concat(slice.apply(arguments, [0])));  //保存调用时传入的参数。
        };
    });
    示例:
    var a = function () {
        console.log(arguments.length);
        return this.a;
    }.bind({
        'a': 'aaa',
        'b': 'bbb'
    }, 222, 444);
    console.log(a(333, 666));
    =>4
    =>'aaa'


    Number

    number.toExponential(fractionDigits);  //把number转换成指数形式字符串。可选参数范围0~20,表示小数点位数,默认15。
    示例:
    console.log(Math.PI.toExponential(5));
    console.log(Math.PI.toExponential(0));  
    console.log(Math.PI.toExponential());  
    console.log(Math.PI.toExponential(21));  //报错不能执行。
    console.log(Math.PI.toExponential(-1));  //报错不能执行。
    =>3.14159e+0
    =>3e+0
    =>3.141592653589793e+0


    number.toFixed(fractionDigits);  //把number转换成十进制形式字符串。可选参数范围0~20,表示小数点位数,默认0。
    示例:
    console.log(Math.PI.toFixed(5));
    console.log(Math.PI.toFixed(0));  
    console.log(Math.PI.toFixed());  
    =>3.14159
    =>3
    =>3


    number.toPrecision(precision);  //把number转换成十进制形式字符串。可选参数范围1~21,表示精度(数字位数),默认16。
    示例:
    console.log(Math.PI.toPrecision(5));
    console.log(Math.PI.toPrecision(1));  
    console.log(Math.PI.toPrecision());  
    =>3.1416  //四舍五入
    =>3
    =>3.141592653589793


    number.toString(radix);  //把number转换成字符串。可选参数范围2~36,表示基数。默认是10。基数可以是小数。
    示例:
    console.log(Math.PI.toString(2));
    console.log(Math.PI.toString(4.5));
    console.log(Math.PI.toString(16));  
    console.log(Math.PI.toString());  
    =>11.001001000011111101101010100010001000010110100011
    =>3.021003331222202020112203
    =>3.243f6a8885a3
    =>3.141592653589793


    Object

    object.hasOwnProperty(name);  //原型链中的同名属性不会被检查,返回值为true/false。
    示例:
    var a = {'ddd': 333};
    var b = {};
    Object.prototype.aaa = 1;
    b.ddd = a.ddd;
    console.log(a.hasOwnProperty('ddd'));
    console.log(b.hasOwnProperty('ddd'));
    console.log(a.hasOwnProperty('aaa'));
    console.log(b.hasOwnProperty('aaa'));
    =>true
    =>true
    =>false
    =>false


    RegExp

    regexp.exec(string);  //成功匹配时返回一个数组,下标0包含匹配的字符串,下标1包含分组1捕获的字符串...。匹配失败返回null。当对正则使用g标识时,则从regexp.lastIndex(初始值为0)位置,开始匹配。匹配成功时,设置新的lastIndex值,匹配失败时,将lastIndex重置为0,regexp本身只调用了一次。该方法强大而慢。
    示例:
    var a = 'abc abc abc';
    var b = /(a)(b)(c)/.exec(a);
    var c = /(a)(b)(c)/g.exec(a);
    console.log(b);
    console.log(c);
    =>['abc', 'a', 'b', 'c']
    =>['abc', 'a', 'b', 'c']

    var a = 'abc abc abc';
    var i = 0;
    var j = 0;
    var temp1, temp2;
    var regexp1 = /(a)(b)(c)/;
    var regexp2 = /(a)(b)(c)/g;
    while (temp1 = regexp1.exec(a)) {//死循环,会导致浏览器崩溃
        i++;
        console.log(i);
        console.log(temp1);
    }
    while (temp2 = regexp2.exec(a)) {//循环结果如下:
        j++;
        console.log(j);
        console.log(temp2);
    }
    =>1
    =>['abc', 'a', 'b', 'c']
    =>2
    =>['abc', 'a', 'b', 'c']
    =>3
    =>['abc', 'a', 'b', 'c']

    regexp.test(string);  //如果该正则匹配string,返回true。否则,返回false。不要对这个方法使用g。该方法简单而快。
    模拟实现:
    Function.prototype.method = function (name, func) {
        this.prototype[name] = func;
        return this;
    };
    RegExp.method('test', function (str) {
        return this.exec(str) !== null;
    };
    示例:
    var  a = /&.+/.test('yyl  ');
    var  b = /&.+/g.test('yyl  ');  //加g,可能影响效率
    console.log(a);
    console.log(b);
    =>true
    =>true

  • 相关阅读:
    部署应用映射外部目录、迁移与备份、容器保存镜像、镜像打压成压缩包、压缩包恢复为镜像、dockerfiles、私有仓库、docker-compose、部署多应用、容器间通信
    docker的介绍、架构图、乌班图、安装、远程仓库、镜像操作、容器操作、应用部署、
    flask-script(制定命令)、sqlschemy、orm的使用、线程安全、增删查改、多表操作、flask-sqlalchemy
    g对象、flask-session、数据库连接池、wtforms(forms组件)、信号
    中间件、猴子补丁、蓝图、请求上下文执行流程
    flask配置文件、模板、request对象的属性和方法、响应对象方法、闪现、session的使用、请求扩展、全局标签、全局过滤器、
    flask入门、配置文件、路由系统、路由的本质、CBV
    基本权限chmod、软件管理-二进制安装、源码安装、进程管理、系统服务、
    对爬取京东商品按照标题为其进行自动分类---基于逻辑回归的文本分类
    学习进度3.16
  • 原文地址:https://www.cnblogs.com/bugong/p/4357552.html
Copyright © 2011-2022 走看看