[英]A bit of help understanding javascript splits
我基本上是想调试一些JavaScript,但我不太了解。
这是代码
if(a&&0<=a.indexOf("://")&&a.split("/")[2]!=f.location.href.split("/")[2])
这是值
一个= "/cc/subs/TOS-turkish.srt"
f.location- "http://192.168.55.108:5555/cc/mypage.html"
这可能是他们应该如何匹配每个对象的情况。 我现在真的很困惑。
谢谢。
调用split
将内容拆分为一个数组。 在"http://192.168.55.108:5555/cc/mypage.html"
上调用split("/")
将为您提供:
["http:", "", "192.168.55.108:5555", "cc", "mypage.html"]
的[2]
索引是"192.168.55.108:5555"
。
if
语句的作用是检查它们是否不匹配:
a.split("/")[2] != f.location.href.split("/")[2]
^
如果a.split("/")[2]
不等于"192.168.55.108:5555"
,则if
语句的这一部分将成功。
在"/cc/subs/TOS-turkish.srt"
上调用split("/")
将为您提供:
["", "cc", "subs", "TOS-turkish.srt"]
因此, if
"subs"
(上述数组的[2]
索引)不等于"192.168.55.108:5555"
,则if
语句的这一部分将成功。
但是 , if
语句将在此之前失败,因为a
没有"://"
,因此a.indexOf("://")
返回-1
并且0
不小于或等于-1
。
要完全分解您的if
语句:
a // true: a = "/cc/subs/TOS-turkish.srt"
&& 0 <= a.indexOf("://") // false: 0 is greater than -1
&& ...[2] != ...[2] // true: "subs" isn't equal to "192.168.55.108:5555"
由于它在0 <= a.indexOf("://")
上返回false
,因此无论如何它永远不会到达最后一部分。
JavaScript .split()
方法允许您通过提供要在其上分割字符串的关键字(或字符)或正则表达式将字符串分割为数组。
例如,在您的情况下,它在字符“ /”上分割。
完成拆分后,您可以按索引直接访问数组中的项目...因此[2]
获取数组中的第三个项目(因为数组的索引为零)
假设您要将此序列号中的数字块放入数组中:
var sn = '123-456-789';
var chunks = sn.split('-');
//chunks is now an array of: ['123', '456', '789']
该比较尝试查看输入URL的域是否与当前页面的域匹配。
"http://stackoverflow.com".split("/")[2]
给出stackoverflow.com
。
该代码可能是脚本的一部分,该脚本查看页面中的链接,并在它们指向非现场资源时对其进行处理,例如:“嘿,您要离开此网站,您确定要继续吗?” 。
如果您显示更多代码,我也许可以给出更明确的答案。
评估为false
,因为:
if (
a && // <-- if a is true
0 <= a.indexOf("://") && // <-- if there is '://' in a
a.split("/")[2] != f.location.href.split("/")[2] // <-- the 2nd part of a, split by '/' (in your case 'subs') is not equal to the 2nd part of f, split by '/' (in your case '192.168.55.108:5555')
) {
do_something();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.