js数组的at方法
2023-09-11 14:19:18 时间
var arr = ["sunwukong", "zhubajie", "tangseng"]
console.log(arr.at(-1)) // tangseng
console.log(arr.at(-2)) // zhubajie
console.log(arr.at(10)) // undefined 超出最大下标,那么就返回undefined,不会报错
如果发现我们当前的浏览器不支持at方法,那么,我们可以使用profill
function at(n) {
// ToInteger() abstract op
n = Math.trunc(n) || 0;
// Allow negative indexing from the end
if (n < 0) n += this.length;
// OOB access is guaranteed to return undefined
if (n < 0 || n >= this.length) return undefined;
// Otherwise, this is just normal property access
return this[n];
}
const TypedArray = Reflect.getPrototypeOf(Int8Array);
for (const C of [Array, String, TypedArray]) {
Object.defineProperty(C.prototype, "at",
{
value: at,
writable: true,
enumerable: false,
configurable: true
});
}
相关文章
- 使用js Math.random()函数生成n到m间的随机数字
- 实现网页页面跳转的几种方法(meta标签、js实现、php实现)
- 原生Js_使用setInterval() 方法实现图片轮播功能
- JS框架_(JQuery.js)夜晚天空满天星星闪烁动画
- JS跨域(ajax跨域、iframe跨域)解决方法及原理详解(jsonp)
- 纯js实现人脸识别眨眨眼张张嘴案例——index.html
- JS中setInterval、setTimeout不能传递带参数的函数的解决方案
- [Vue] Create Vue.js Layout and Navigation with Nuxt.js
- vue.js 3.2.22:平滑回到顶部
- js中setTimeout、setInterval、 clearInterval方法简介
- node.js版本管理for window ---- nvmw
- Js apply方法详解
- [JS Compse] 4. A collection of Either examples compared to imperative code
- [Whole Web] [Node.js] Using npm run to launch local scripts
- JS中实现页面跳转和刷新方法总结
- atitit.判断时间重叠方法总结 java c++ c#.net js php
- windows 环境下使用 Node.js 访问 SAP OData 遇到 unable to get local issuer certificate
- 原生js实现随机验证码HTMl-JS
- js多个文件格式的判断并处理方法
- js使浏览器窗口最大化(适用于IE的方法)
- js数组push方法使用注意
- js之敏感词过滤
- js-jquery-SweetAlert2【二】配置与方法
- JS工具方法 1 用xpath处理字符串查找
- satellite.js库下载、介绍、安装、引用,返回函数的方法