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 ...
随机推荐
- Spark-shell 无法启动之网络问题
由于需要首次手动安装sbt,需要联网,故将虚拟机的网络适配器模式设置为"桥接模式",这样就可以和互联网相连接. 但是后面执行"spark-shell --master ...
- Openresty配置文件上传下载
1. 下载包安装Openresty openresty-1.13.6.1下载地址 https://openresty.org/download/openresty-1.13.6.1.tar.gz 安装 ...
- 深入浅出FPGA-2-让source insight 支持verilog HDL
引言 工欲善其事,必先利其器.最近用verilog在FPGA上做一个简易的计算器,整个工程由6个模块组成,之前写的代码都是很小的,模块也很少,一般就一两个,没感觉出别扭.但是模块多了就发现,模块之间的 ...
- C#新特性, dynamic, ExpandObject
http://msdn.microsoft.com/en-us/library/system.dynamic.expandoobject.aspx using System; using System ...
- Django项目国际化
Django项目国际化 实验环境: py3.4.3 + django1.8.2 + Windows 项目设置 >django-admin startproject I18nDjango > ...
- builtroot make menuconfig流程
本文主要介绍一下,buildroot(buildroot-2018.02.1)的make menuconfig.众所周知,在我们执行menuconfig时,会生成一个图形化界面,然后进行相关的配置.同 ...
- RTFSC-afinal框架[一]
RTFSC-afinal框架 finalActivity模块 : android中的ioc框架,完全注解方式就可以进行UI绑定和事件绑定.无需findViewById和setClickListen ...
- superobject 序列数据集
unit uDBJson; interface {$HINTS OFF} uses SysUtils, Classes, Variants, DB, DBClient, SuperObject; ty ...
- Java反射小结
一.什么是反射? 在运行状态中,对于任意一个类,都能够获取到这个类的所有属性和方法,对于任意一个对象,都能够调用它的任意一个方法和属性(包括私有的方法和属性),这种动态获取的信息以及动态调用对象的方法 ...
- idea编译修改jdk版本
由于项目需要,需要修改jdk的版本为1.8,这里记录一下修改的地方 1,项目的jdk版本 右键点击项目-> open module setting ->project,然后如图所示 2,在 ...