split 函数是什么样的?
我遇到了这个声明:
userName = document.cookie.split("=")[1];
在这里在 w3schools 阅读了 split 声明之后。这表示 split 的语法是 string.split(分隔符,限制)
。那么第一个括号后面的方括号是做什么的。意思是 ? 如果这是真的,split
函数是什么样的?
I came across this statement :
userName = document.cookie.split("=")[1];
after reading about split statement here at w3schools. which says that syntax of split isstring.split(separator, limit)
. Then what does the square bracket after first parens. mean ?
If this is true what does split
function look like ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
String.split(separator, limit)
返回一个数组。在 Javascript 中,您可以使用方括号通过索引访问数组值。数组从零开始,0
是第一个元素,1
是第二个元素,依此类推。与您的代码等效的是:
这用等号 (
=
) 分隔document.cookie
并从中获取第二个元素(索引 1)。document.cookie
是document
对象的一个特殊属性(数据类型:字符串),它包含网页的所有 cookie,由;
字符分隔。例如,如果 document.cookie 包含name=Adam
,则数组arr
将包含值name
和Adam
。第二个存储在userName
中。请注意,如果 cookie 包含多个值,或者值包含多个等号,则它将不起作用。考虑以下情况:
document.cookie
containsname=Adam; home=无处
。使用上面的代码,这将使userName
包含Adam; home
因为字符串是用等号分隔的,然后取第二个值。document.cookie
包含home=Nowhere;名称=亚当
。这将导致userName
包含Nowhere; name
document.cookie
包含name=Adam=cool
。在本例中,userName
将为Adam
而不是Adam=cool
。另外,w3schools 不太可靠。使用更权威的来源,例如 Mozilla 开发者网络:
document.cookie
String.split
String.split(separator, limit)
returns an array. In Javascript, you can access array values by index using the square brackets. Arrays are zero-based,0
is the first element,1
the second and so on.The equivalent of your code would be:
This separates the
document.cookie
by the equal-sign (=
) and takes the second element (index 1) from it.document.cookie
is a special property (datatype: String) of thedocument
object which contains all cookies of a webpage, separated by the;
character. E.g. if document.cookie containsname=Adam
, the arrayarr
will contain the valuesname
andAdam
. The second one is stored inuserName
.Note that if the cookie contains multiple values, or if the value contains multiple equal-signs, it won't work. Consider the next cases:
document.cookie
containsname=Adam; home=Nowhere
. Using the above code, this would makeuserName
containAdam; home
because the string is separated by the equal-sign, and then the second value is taken.document.cookie
containshome=Nowhere; name=Adam
. This would result inuserName
containingNowhere; name
document.cookie
containsname=Adam=cool
. In this case,userName
would beAdam
and notAdam=cool
.Also, w3schools is not that reliable. Use more authorative sources like the Mozilla Developer Network:
document.cookie
String.split
split 函数返回按给定分隔符分割的字符串数组。使用方括号,您可以访问该(返回的)数组的第 n 个元素。
如果您熟悉 Java,它的行为与 Java 中的 String.split() 方法相同。
The
split
function returns anarray
of strings split by the given separator. With the square bracket you are accessing the nth element of that (returned) array.If you are familiar with Java, its the same behavior as the String.split() method there.
它获取结果数组的第二个索引
相同:
It gets the second index of the resulting array
Same as:
split 返回一个字符串数组。所以方括号意味着从返回的数组中获取第二个字符串。
split returns an array of strings. So square brackets mean get second string from the returned array.
您提供的代码中的方括号用于访问 split() 返回的数组的第二个元素。该函数本身返回一个数组。该代码与以下内容相同:
The square bracket in the code you supplied is accessing the second element of the array returned by
split()
. The function itself returns an array. That code would be the same as:Split 将返回一个数组,例如 [1, 2, 3]。如果您在其后提供方括号,它将返回括号中指定的键,在这种情况下 userName 将为 2
Split would return an array e.g. [1, 2, 3]. If you supply the square bracket after it, it will return the specified key in the brackets, in this case userName would be 2
你不应该使用w3schools,但是......
在JavaScript中,函数参数是可选的,并且可以提供更少的参数参数比函数预期的要多。函数中的额外参数是未定义的。有些函数被编程来处理这种可能性,
string.split
就是其中之一。另一部分与 split 返回一个数组这一事实有关。然后可以使用方括号表示法对数组进行索引,因此在函数调用之后使用
[1]
。You shouldn't be using w3schools, but...
In JavaScript, function parameters are optional and it is possible to supply fewer parameters than the function expects. The extra parameters in the function are then undefined. Some functions are programmed to deal with that possibility and
string.split
is one of them.The other part has to do with the fact that
split
returns an array. Arrays can then be indexed using the square bracket notation, hence the[1]
after the function call.