实现说明
演示用的数据放在这几个模块级绑定中:events、path。
使用要点
- 每一步打印都带着自己的标签:
last push:、first push:、absent:。
常见问题
有第三方依赖吗?
没有。依赖清单是空的,代码只用标准库。 状态都挂在模块顶层:events、path。
核心语句
const events = ['push', 'test', 'push', 'lint', 'push'];
const path = 'assets/img/logo.svg';
代码示例
// lastIndexOf scans from the tail toward the head and returns the last matching index
const events = ['push', 'test', 'push', 'lint', 'push'];
console.log('last push:', events.lastIndexOf('push'));
console.log('first push:', events.indexOf('push'));
console.log('absent:', events.lastIndexOf('deploy'));
// the second argument is the position to start scanning from (inclusive)
console.log('from 2:', events.lastIndexOf('push', 2));
console.log('from -2:', events.lastIndexOf('push', -2));
// negatives are normalized as length + fromIndex; still negative after that skips the whole array and returns -1
console.log('from -100:', events.lastIndexOf('push', -100));
// typical use: locate the last path separator
const path = 'assets/img/logo.svg';
console.log('slash at:', path.lastIndexOf('/'));
console.log('dir:', path.slice(0, path.lastIndexOf('/')));