新技术篇 - Array.prototype.at()函数

相对索引方法之.at函数

Array.prototype 上的新的新方法 --- at 函数,可以让我们更加方便的访问到数组 和 字符串末尾的元素。

在实际开发中,我经常需要去访问到数组或者是字符串中末尾的某个元素的。

但是吧,通常使用的法子不是很好的,比如 my_array[my_array.length - N]

或者是

使用可能没有什么性能的操作,比如 my_array.slice(-N)[0]

使用这个新的函数方法 .at() ,它更加符合我们人自身的常规思考。它是这样的,它将负指数解释为“从最后”。

什么意思呢?

就是,参数是负数,就表示倒数第几个; 正数就表示正数第几个,相当于普通的属性访问。

前面的示例可以表示为 my_array.at(-N)

比如:

var arr = [125978]

// 第0个
console.log(arr.at(0)) // 1

// 正数第一个
console.log(arr.at(1)) // 2

// 倒数第一个
console.log(arr.at(-1)) // 78

// 倒数第二个
console.log(arr.at(-2)) // 9

是不是很符合我们的思考逻辑呢?

这个新方法足够小,它的完整语义可以被下面这个兼容的 polyfill 实现理解:

function at(n{
  // Convert the argument to an integer
  n = Math.trunc(n) || 0// 去掉小数点
  // Allow negative indexing from the end
  if (n < 0) n += this.length;
  // Out-of-bounds access returns undefined
  if (n < 0 || n >= this.length) return undefined;
  // Otherwise, this is just normal property access
  return this[n];
}

对字符串的补充

由于 at 最终执行普通索引,因此在字符串值上调用 at 会返回对应索引的字符。 就像字符串上的普通索引一样,这返回的字符可能不是您想要的 Unicode 字符! 请考虑 String.prototype.codePointAt() 是否更适合您的用例。

使用 codePointAt():

'ABC'.codePointAt(1);          // 66
'\uD800\uDC00'.codePointAt(0); // 65536

'XYZ'.codePointAt(42); // undefined

动下小手

  • 欢迎关注我的GitHub:@huangyangquang ⭐⭐
  • 欢迎关注我的公众号:前端学长Joshua
05-23 16:00:49.311 25132-25512 A0c0d0/JSAPP I result {"code":200,"msg":"操作成功","data":[{"id":1,"name":"华为Mate 60 Pro","model":"ALN-AL80","imageUrl":"https://img.alicdn.com/imgextra/i3/2213047591560/O1CN01QNx6eB1NOWu3oeUfn_!!2213047591560.jpg_q50.jpg_.webp","shortDescription":"麒麟9000S 卫星通信","price":6999.00,"subsidizedPrice":6499.00,"sales":15000,"rating":4.90,"stock":500,"isAvailable":0,"brand":"华为"},{"id":2,"name":"华为P60 Art","model":"NOH-AN80","imageUrl":"https://img.alicdn.com/imgextra/i4/2609173245/O1CN011h2L1I1ZqGCdlsYlJ_!!2609173245.jpg_q50.jpg_.webp","shortDescription":"超聚光XMAGE影像","price":7988.00,"subsidizedPrice":7588.00,"sales":12000,"rating":4.80,"stock":300,"isAvailable":0,"brand":"华为"},{"id":3,"name":"华为nova 12","model":"BAC-AL00","imageUrl":"https://picasso.alicdn.com/imgextra/O1CNA1bnbda21Vuba4TjLYB_!!2838892713-0-psf.jpg_.webp","shortDescription":"前置双摄人像","price":2999.00,"subsidizedPrice":2699.00,"sales":50000,"rating":4.70,"stock":1000,"isAvailable":0,"brand":"华为"},{"id":4,"name":"荣耀Magic6 Pro","model":"PGT-AN30","imageUrl":"https://img.alicdn.com/imgextra/i2/263726286/O1CN01ko93gt1wJ2hQAORAB_!!4611686018427380942-0-item_pic.jpg_.webp","shortDescription":"骁龙8 Gen3 青海湖电池","price":5699.00,"subsidizedPrice":5399.00,"sales":25000,"rating":4.80,"stock":600,"isAvailable":0,"brand":"荣耀"},{"id":5,"name":"荣耀X50 GT","model":"ANY-AN00","imageUrl":"https://img.alicdn.com/imgextra/i3/3851598352/O1CN01UHPfyv2BZGwPnHUlx_!!0-item_pic.jpg_q50.jpg_.webp","shortDescription":"1.5K护眼曲屏","price":1999.00,"subsidizedPrice":1799.00,"sales":80000,"rating":4.60,"stock":1500,"isAvailable":0,"brand":"荣耀"},{"id":6,"name":"荣耀90 Pro","model":"REA-AN20","imageUrl":"https://img.alicdn.com/imgextra/i3/1114511827/O1CN01r3ngr41PMof7MhFhY_!!4611686018427386323-0-item_pic.jpg_q50.jpg_.webp","shortDescription":"2亿像素写真相机","price":3299.00,"subsidizedPrice":2999.00,"sales":40000,"rating":4.70,"stock":800,"isAvailable":0,"brand":"荣耀"},{"id":7,"name":"荣耀Play7T Pro","model":"CMA-AN40","imageUrl":"https://img.alicdn.com/imgextra/i1/1907069314/O1CN019jd3kt2IfrnZ6J0AH_!!1907069314.jpg_q50.jpg_.webp","shortDescription":"40W快充 OLED屏","price":1799.00,"subsidizedPrice":1599.00,"sales":120000,"rating":4.50,"stock":2000,"isAvailable":0,"brand":"荣耀"},{"id":8,"name":"华为畅享70 Pro","model":"CMA-AN70","imageUrl":"https://android-api.oss-cn-beijing.aliyuncs.com/logo.jpg","shortDescription":"7000mAh长续航","price":1599.00,"subsidizedPrice":1399.00,"sales":150000,"rating":4.40,"stock":3000,"isAvailable":0,"brand":"华为"},{"id":9,"name":"华为Mate X5","model":"TET-AN80","imageUrl":"https://android-api.oss-cn-beijing.aliyuncs.com/logo.jpg","shortDescription":"折叠屏 玄武钢化","price":12999.00,"subsidizedPrice":12499.00,"sales":5000,"rating":4.90,"stock":100,"isAvailable":0,"brand":"华为"},{"id":10,"name":"荣耀Magic Vs2","model":"CMA-AN80","imageUrl":"https://android-api.oss-cn-beijing.aliyuncs.com/logo.jpg","shortDescription":"轻薄折叠屏","price":8999.00,"subsidizedPrice":8499.00,"sales":10000,"rating":4.70,"stock":300,"isAvailable":0,"brand":"荣耀"},{"id":11,"name":"iPhone 15 Pro Max","model":"A3108","imageUrl":"https://android-api.oss-cn-beijing.aliyuncs.com/logo.jpg","shortDescription":"A17 Pro 钛金属","price":9999.00,"subsidizedPrice":null,"sales":80000,"rating":4.80,"stock":200,"isAvailable":0,"brand":"苹果"},{"id":12,"name":"iPhone 15","model":"A3092","imageUrl":"https://android-api.oss-cn-beijing.aliyuncs.com/logo.jpg","shortDescription":"A16芯片 灵动岛","price":5999.00,"subsidizedPrice":null,"sales":150000,"rating":4.80,"stock":5000,"isAvailable":0,"brand":"苹果"},{"id":13,"name":"iPhone 15 Plus","model":"A3096","imageUrl":"https://android-api.oss-cn-beijing.aliyuncs.com/logo.jpg","shortDescription":"6.7英寸大屏","price":6999.00,"subsidizedPrice":null,"sales":80000,"rating":4.70,"stock":3000,"isAvailable":0,"brand":"苹果"},{"id":14,"name":"iPhone SE 4","model":"A2785","imageUrl":"https://android-api.oss-cn-beijing.aliyun 05-23 16:00:49.311 25132-25512 A0c0d0/JSAPP I result [object Object] 05-23 16:00:49.359 25132-25512 C03f00/ArkCompiler E [ArkRuntime Log] TypeError: is not callable 05-23 16:00:49.361 25132-25512 C03900/Ace E [Engine Log]Lifetime: 0.000000s 05-23 16:00:49.361 25132-25512 C03900/Ace E [Engine Log]Js-Engine: ark 05-23 16:00:49.361 25132-25512 C03900/Ace E [Engine Log]page: pages/Index.js 05-23 16:00:49.361 25132-25512 C03900/Ace E [Engine Log]Error message: is not callable 05-23 16:00:49.361 25132-25512 C03900/Ace E [Engine Log]Stacktrace: 05-23 16:00:49.361 25132-25512 C03900/Ace E [Engine Log] at forEachUpdateFunction (/devcloud/slavespace/usr1/081f8aba80800f0f0fcec015bd66c7e0/harmony_code/codearts_workspace/foundation/arkui/ace_engine/frameworks/bridge/declarative_frontend/engine/stateMgmt.js:4352:1) 05-23 16:00:49.361 25132-25512 C03900/Ace E [Engine Log] at anonymous (entry|entry|1.0.0|src/main/ets/pages/Index.ts:144:13) 05-23 16:00:49.361 25132-25512 C03900/Ace E [Engine Log] at updateFunc (/devcloud/slavespace/usr1/081f8aba80800f0f0fcec015bd66c7e0/harmony_code/codearts_workspace/foundation/arkui/ace_engine/frameworks/bridge/declarative_frontend/engine/stateMgmt.js:6812:1) 05-23 16:00:49.361 25132-25512 C03900/Ace E [Engine Log] at UpdateElement (/devcloud/slavespace/usr1/081f8aba80800f0f0fcec015bd66c7e0/harmony_code/codearts_workspace/foundation/arkui/ace_engine/frameworks/bridge/declarative_frontend/engine/stateMgmt.js:6514:1) 05-23 16:00:49.361 25132-25512 C03900/Ace E [Engine Log] at anonymous (/devcloud/slavespace/usr1/081f8aba80800f0f0fcec015bd66c7e0/harmony_code/codearts_workspace/foundation/arkui/ace_engine/frameworks/bridge/declarative_frontend/engine/stateMgmt.js:6741:1) 05-23 16:00:49.361 25132-25512 C03900/Ace E [Engine Log] at updateDirtyElements (/devcloud/slavespace/usr1/081f8aba80800f0f0fcec015bd66c7e0/harmony_code/codearts_workspace/foundation/arkui/ace_engine/frameworks/bridge/declarative_frontend/engine/stateMgmt.js:6736:1) 05-23 16:00:49.361 25132-25512 C03900/Ace E [Engine Log] at rerender (entry|entry|1.0.0|src/main/ets/pages/Index.ts:152:9)
最新发布
05-24
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值