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 ...
随机推荐
- DiskLrucCache使用Demo(强烈推荐,非常好用)
DiskLrucCache使用的Demo,这个demo是从网络获取一张图片,保存到本地缓存中(sdcard和内存),当下载成功后.再打开不会又一次向网络请求图片.而是世界使用本地资源. 要使用Disk ...
- 安装yii2高级应用模板
composer global require "fxp/composer-asset-plugin:1.0.0" composer create-project --prefer ...
- #include""和#include<>的区别
一般来说,就是搜索路径不同 #include ""先去搜用户当前路径(也就是调用编译器的路径),然后再去搜用户用-I选项指定的路径,最后再去搜索编译器默认指定的路径(也就是所谓的系 ...
- oracle,mysql分页
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-/ ...
- Action方法调用
一.Action访问路径 Action的访问路径是由struts.xml文件中配置的Action所在包的命名空间,Action的名字和常struts.action.extension共同决定的 例如: ...
- HttpClient+jsoup登录+解析 163邮箱
找了几个,只有这个靠谱,用的是httpclient4,另外还需要commons-lang和jsoup包 http://jsoup.org/ http://www.oschina.net/code/sn ...
- FireFox插件FirePHP调试PHP
如果你和我一样,你会在开发网页项目时候完全无法离开FireBug.这个小巧的"臭虫"是一个神奇而有用的HTML/CSS/JavaScript/Ajax调试器.但是你也许不知道这个还 ...
- Crosses Puzzles zoj 4018 (zju校赛)
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5746 题目大意: N*M的方格里,每个格子有一个指针,一开始指向上下左右四个方 ...
- js创建form添加input项目并提交表单
var generateHideElement = function (name, value) { var tempInput = document.createElement("inpu ...
- EasyUI简单CRUD
<!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml"><head> < ...