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

posted @ 2015-03-22 16:37  bugong  阅读(182)  评论(0编辑  收藏  举报