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
2015-03-22——js常用其它方法的更多相关文章
- JS常用校验方法(判断输入框是否为空,数字,电话,邮件,四舍五入等)
JS常用校验方法: 1.判断输入框是否为空,为空时弹出提示框 2.关闭窗口 3.检查输入字符串是否为数字 4.强制把大写转换成小写 5.手机号码校验,长度为11位数字. 6.电子邮件校验 7.电话号码 ...
- 【js常用DOM方法】
介绍几个js DOM的常用方法 获取元素节点 getElementById getElementsByTagName getElementsByClassName 先写一个简单的网页做测试: /* ...
- JS常用公共方法封装
_ooOoo_ o8888888o 88" . "88 (| -_- |) O\ = /O ____/`---'\____ .' \\| |// `. / \\||| : |||/ ...
- js常用通用方法
验证身份证详细方法 function isCardNo(pId) { var arrVerifyCode = [1, 0, "x", 9, 8, 7, 6, 5, 4, 3, 2] ...
- 一些JS常用的方法
/** * JS公用类库文件 */ (function(){ Tools = { W: window, D: document, Postfix: ".php", GetId: f ...
- Node.js 常用Mongoose方法
Node.js 手册查询-Mongoose 方法 一.Schema 一种以文件形式存储的数据库模型骨架,无法直接通往数据库端,也就是说它不具备对数据库的操作能力.可以说是数据属性模型(传统意义的表结构 ...
- JS常用属性方法大全
1. 输出语句 : document.write(""); 2.JS 中的注释为 : // 3. 传统的 HTML 文档顺序是 : document->html->(h ...
- js常用API方法
String对象常用的API:API指应用程序编程接口,实际上就是一些提前预设好的方法. charAt() 方法可返回指定位置的字符. stringObject.charAt(index) index ...
- js常用共同方法
var uh_rdsp = (function(){ //获取根目录 var getContextPath = function(){ var pathName = document.location ...
- Node.js常用express方法
Node.js 手册查询-Express 方法 1.send方法 send 方法向浏览器发送一个响应信息,并可以智能处理不同类型的数据 send方法在输出响应时会自动进行一些设置,比如HEAD信息.H ...
随机推荐
- debian下运行netstat失败
如果提示:bash: netstat: command not found 说明没有安装netstat工具,而该工具在 net-tools 工具包内. apt-get install net-tool ...
- atitit.javascript js 上传文件的本地预览
atitit.javascript js 上传文件的本地预览 1. .URL.createObjectURL 1 1.1. 吊销所有使用 URL.createObjectURL 而创建的 URL,以 ...
- cocoa pods 升级遇到的问题
1. cocoa pods 升级遇到的问题 运行 sudo gem update --system 报错 Updating rubygems-update ERROR: While executing ...
- xgboost 安装
git clone --recursive https://github.com/dmlc/xgboostcd xgboost/make -j4 cd python-package/ python s ...
- HTML5与XML的区别
XHTML 是 XML 风格的 HTML 4.01. HTML5 是HTML的第五大版本,取代 HTML 4.01. XHTML是基于XML发布的HTML规范,旨在规范HTML的格式. 两者提出的目的 ...
- HTML5 的位置
HTML5 的位置 在HTML5COL学院的前面几个章节中我们已经对HTML5 Geolocation API有了一定认识,接下来我们要对位置做些更有意思的处理:看看你与我们HTML5COL学院的办公 ...
- PHP从千千静听服务器获取lrc歌词
<?php //转载请注明出处 uenucom function SingleDecToHex($dec) { $tmp=""; $dec=$dec%16; if($ ...
- linx 设备名字来由 sd sr sg st
转载保留:http://blog.csdn.net/luoweifeng1989/archive/2011/05/17/6426193.aspx 一直不知道 sda sdb sr0 这些名字的前两两个 ...
- InnoDB: auto-extending data file ./ibdata1 is of a different size 640 pages (rounded down to MB) than specified in the .cnf file: initial 768 pages, max 0 (relevant if non-zero) pages!
问题描述: centos 安装MySQL $yum install mysql-server 安装之后执行命令mysql 报错: 查看mysql的启动日志: [ERROR] InnoDB: auto- ...
- 用node.js进行微信公众平台的开发
基本原理 用nodejs怎样来实现对微信公众平台的开发呢? 别的就不多说了,先来简单介绍微信公众平台的基本原理. 微信服务器就相当于一个转发服务器,终端(手机.Pad等)发起请求至微信服务器,微信服务 ...