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 ...
随机推荐
- 解决Ubuntu下博通网卡驱动问题
sudo apt-get install bcmwl-kernel-source我的无线网卡是BCM4312 802.11b/g 在12.04 会集成驱动,装完就能用,现在用14.04,16.04里面 ...
- Atitit.png 图片不能显示 php环境下
Atitit.png 图片不能显示 php环境下 1.1. 不能显示png 下载png 检查使用bcompare与正常png对比.. 多了bom头 , "\xEF\xBB\xBF" ...
- Sublime Text 2 入门与总结
Sublime Text 2 入门与总结 首语 : 考完试,但又没什么兴趣做课程设计,蛋疼的弄点软件入门的介绍,希望给各位还在吃香蕉的程序猿带来一点启示... 代码编辑器,就像武侠中的武 ...
- 关于linux PPA源问题
添加PPA: 1.首先进入ubuntu系统,system—>administration—>update manager—>setting,在软件源界面,点击other softwa ...
- 什么是ETag
ETag 是 Entity Tag 的缩写,中文译过来就是实体标签的意思.在HTTP1.1协议中其实就是请求HEAD中的一个属性而已. HTTP/1.1 200 OK Date: Mon, 23 Ma ...
- MySQL 记录中包含换行符
换行符: \r\n \r CHAR(10) 处理方法: REPLACE(str,from_str,to_str); 如:REPLACE('aaa\nbbb','\n','');
- linux学习笔记18--文件/chmod/chown/chgrp
文件类型与扩展名: Linux文件类型和Linux文件的文件名所代表的意义是两个不同的概念.我们通过一般应用程序而创建的比如file.txt.file.tar.gz ,这些文件虽然要用不同的程序来打开 ...
- ASP.NET中常用的26个优化性能方法
1. 数据库访问性能优化 数据库的连接和关闭 访问数据库资源需要创建连接.打开连接和关闭连接几个操作.这些过程需要多 次与数据库交换信息以通过身份验证,比较耗费服务器资源.ASP.NET 中提供了 连 ...
- 假设web应用的文档根目录为MyApp,那么可以从哪里找到database.jar文件。
假设web应用的文档根目录为MyApp,那么可以从哪里找到database.jar文件. A. MyApp目录下 B. MyApp\images目录下 C. MyApp\WEB-INF目录下 D. M ...
- Codeforces 193A. Cutting Figure
看起来非常神,但仅仅有三种情况 -1 , 1 ,2..... A. Cutting Figure time limit per test 2 seconds memory limit per test ...