javascript 方法总结

本文深入探讨了JavaScript中Array对象的各种方法,包括concat、join、pop、push、reverse、shift、slice、sort、splice、unshift等,并通过实例展示了它们的使用方式。此外,还介绍了number对象的toExponential、toFixed、toPrecision、toString方法,以及RegExp对象的exec方法,String对象的charAt、charCodeAt、concat、indexOf、lastIndexOf、match、replace方法。文章旨在帮助开发者更好地理解和运用这些内置方法,提高代码效率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Array
1.array.concat(item...)
instance:
    var a = ['2','3'];
    var b = ['4','5'];
    var c = a.concat(b,true);//c = ['2','3','4','5','true'];

2.array.join(separator)
instance:
    var a = ['1','2'];
    a.push('3');
    var c = a.join(',');//c:'1,2,3'
3.array.pop():remove last element in the array,it can be inplemented like that:
    Array.method('pop',function(){
        return this.splice(this.length - 1,1)[0];   
    })
4.array.push(item...):this method can push the element in the array,it can be implemented like that :
    Array.method('push',function(){
        this.splice.apply(
            this,
            [this.length,0]    .concat(Array.prototype.slice.apply(arguments)));
            return this.length;   
    });
5.array.reverse():反向排序
6.array.shift():remove the first element in the array,if the array is null then the method will return undefined
    Array.method('shift',function(){
        return this.splice(0,1)[0];
    })
7.array.slice(start,end):被复制的元素的end-1位。
8.array.sort:正向排序
9.array.splice(start,deletecount,item):移除一个或者多个的元素,用item里面的元素来替换
10.array.unshift(item...):add the element in the first of the array.

11.FUNCTION:
    function.apply(thisArg,argArray):apply调用function函数,传递一个将被绑定到this上的对象和一个可选的参数数组。
instance:
12.NUMBER:
    number.toExponential(fractionDigits):将number转换成指数型的字符串,可选参数fractionDigits控制其小数点后的数字位数,他的值必须在0~20之间
    .toFixed(fractionDigits):转成十进制的字符串
    .toPrecision(fractionDigits):可以控制输出的十进制的字符串的精度,fractionDigits代表精度值
    .toString(fractionDigits):fractionDigits是基数。
13.RegExp
    .exec(string):string是正则表达式的匹配因子。
14.String
    .charAt(pos):返回pos所指定的位置的元素
    .charCodeAt(pos):返回pos所指定的位置的元素的ASIIC
    .concat(String..):连接两个字符串
    .indexof(searchString,postition):从头开始找
    .lastindexof(searchString,pos):从尾部开始找
    .match(Regxp):匹配一个字符串和一个正则表达式
    .replace(searchValue,replaceValue):替换的方法

转载于:https://www.cnblogs.com/ComputerG/archive/2010/04/08/1706942.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值