String 类型的使用
let str = 'orange';
str.indexOf('o'); //0
str.indexOf('n'); //3
str.indexOf('c'); //-1Array
类型的使用
let arr = ['orange', '2016', '2016'];
arr.indexOf('orange'); //0
arr.indexOf('o'); //-1Number
类型无法使用
let num = 2016;
num.indexOf(2); //Uncaught TypeError: num.indexOf is not a function
非要对 number 类型使用
indexOf 方法嘞?那就转换成字符串咯,接着上例来写
//二逼青年的写法
num = '2016';
num.indexOf(2); //0
//普通青年的写法
num.toString().indexOf(2); //0
//文艺青年的写法
('' + num).indexOf(2); //0
本文介绍了JavaScript中不同数据类型如何使用indexOf方法,并展示了字符串、数组及数字类型的具体应用实例。
1996

被折叠的 条评论
为什么被折叠?



