JS 开发问题:url.includes is not a function
在 JavaScript 开发中出现如下错误信息Uncaught TypeError: url.includes is not a function问题原因这个错误是尝试调用 url 的 includes 方法但 url 不是一个字符串问题复现例如url 是一个数字leturl123;if(url.includes(test)){console.log(Found!);}else{console.log(Not found!);}# 输出结果 Uncaught TypeError: url.includes is not a function ...例如url 是一个对象leturl{href:https://test.com};if(url.includes(test)){console.log(Found!);}else{console.log(Not found!);}# 输出结果 Uncaught TypeError: url.includes is not a function ...注意事项如果 url 为 null则会出现如下错误信息leturlnull;if(url.includes(test)){console.log(Found!);}else{console.log(Not found!);}# 输出结果 Uncaught TypeError: Cannot read properties of null (reading includes) ...如果 url 为 undefined则会出现如下错误信息leturlundefined;if(url.includes(test)){console.log(Found!);}else{console.log(Not found!);}# 输出结果 Uncaught TypeError: Cannot read properties of undefined (reading includes) ...处理策略异常捕获处理try{leturl{href:https://test.com};if(url.includes(test)){console.log(Found!);}else{console.log(Not found!);}}catch(error){console.log(error);}# 输出结果 TypeError: url.includes is not a function ...确保 url 是字符串leturl{href:https://test.com};if(typeofurlstringurl.includes(test)){console.log(Found!);}else{console.log(Not found!);}# 输出结果 Not found!
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2425948.html
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!