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

2015-03-22——js常用其它方法的更多相关文章

  1. JS常用校验方法(判断输入框是否为空,数字,电话,邮件,四舍五入等)

    JS常用校验方法: 1.判断输入框是否为空,为空时弹出提示框 2.关闭窗口 3.检查输入字符串是否为数字 4.强制把大写转换成小写 5.手机号码校验,长度为11位数字. 6.电子邮件校验 7.电话号码 ...

  2. 【js常用DOM方法】

    介绍几个js DOM的常用方法 获取元素节点 getElementById  getElementsByTagName  getElementsByClassName 先写一个简单的网页做测试: /* ...

  3. JS常用公共方法封装

    _ooOoo_ o8888888o 88" . "88 (| -_- |) O\ = /O ____/`---'\____ .' \\| |// `. / \\||| : |||/ ...

  4. js常用通用方法

    验证身份证详细方法 function isCardNo(pId) { var arrVerifyCode = [1, 0, "x", 9, 8, 7, 6, 5, 4, 3, 2] ...

  5. 一些JS常用的方法

    /** * JS公用类库文件 */ (function(){ Tools = { W: window, D: document, Postfix: ".php", GetId: f ...

  6. Node.js 常用Mongoose方法

    Node.js 手册查询-Mongoose 方法 一.Schema 一种以文件形式存储的数据库模型骨架,无法直接通往数据库端,也就是说它不具备对数据库的操作能力.可以说是数据属性模型(传统意义的表结构 ...

  7. JS常用属性方法大全

    1. 输出语句 : document.write(""); 2.JS 中的注释为 : // 3. 传统的 HTML 文档顺序是 : document->html->(h ...

  8. js常用API方法

    String对象常用的API:API指应用程序编程接口,实际上就是一些提前预设好的方法. charAt() 方法可返回指定位置的字符. stringObject.charAt(index) index ...

  9. js常用共同方法

    var uh_rdsp = (function(){ //获取根目录 var getContextPath = function(){ var pathName = document.location ...

  10. Node.js常用express方法

    Node.js 手册查询-Express 方法 1.send方法 send 方法向浏览器发送一个响应信息,并可以智能处理不同类型的数据 send方法在输出响应时会自动进行一些设置,比如HEAD信息.H ...

随机推荐

  1. 浅谈 Objective-C 下对象的初始化

    转自:http://www.oschina.net/question/54100_32468 众所周知,Objective-C是一门面向对象的语言,一般情况下,我们在Objective-C中定义一个类 ...

  2. poj2392 Space Elevator(多重背包问题)

    Space Elevator   Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8569   Accepted: 4052 ...

  3. Archive for required library xx cannot be read or is not a valid ZIP file

    原因:maven下载的jar包有问题,导致maven编译的时候出错 解决方法:找到jar包所在的文件路径,在网上重新下载个相同版本的jar包,问题解决

  4. Ubuntu 启动项、菜单 改动 防止隐藏

    因为电脑有多个系统,默认的grub引引导菜单是隐藏的,须要略微改动下方可显示 不要直接改动boot/grub/grub.cfg  要直接改动/etc/default/grub,然后update-gru ...

  5. jQuery 实战读书笔记之第四章:使用特性、属性和数据

    使用属性 /* 每个元素都有一或多个特性,,这些特性的用途是给出相应元素或其内容的附加信息.(出自 JavaScript 高级程序设计) */ /* 特性是固有的 JavaScript 对象 属性指的 ...

  6. EMMC与nand flash的区别

    1.NAND Flash 是一种存储介质,要在上面读写数据,外部要加主控和电路设计. 2.eMMC是NAND flash+主控IC ,对外的接口协议与SD.TF卡类似:对厂家而言简化了电路设计,降低了 ...

  7. mongodb数据库安装与卸载

    此处以centos下monggodb3.4版本安装为例,可参考官网安装教程 步骤如下: 1.配置mongodb ym源 vi /etc/yum.repos.d/mongodb-org-3.4.repo ...

  8. 批量上传文件到HDFS的Shell脚本

    在做Hadoop数据挖掘项目的时候,我们第一步是源数据的获取,即把相应的数据放置到HDFS中,以便Hadoop进行计算,手动将文件上传到HDFS中,未免太费时费力,所以我们可以采取像Flume一样的框 ...

  9. C++ Scoket的升级版(多态的运用)

    //Socket报文发送c++升级版 #define _CRT_SECURE_NO_WARNINGS #include<iostream> using namespace std; #in ...

  10. hpfeeds协议解析

    一. hpfeeds协议简介 hpfeeds是一个轻量级的验证发布-订阅协议(authenticated publish-subscribe protocol). 发布-订阅协议:发布/订阅协议定义了 ...