split 函数是什么样的?

发布于 2024-12-05 17:47:54 字数 307 浏览 0 评论 0原文

我遇到了这个声明:

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 is
string.split(separator, limit). Then what does the square bracket after first parens. mean ?
If this is true what does split function look like ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

娇柔作态 2024-12-12 17:47:54

String.split(separator, limit) 返回一个数组。在 Javascript 中,您可以使用方括号通过索引访问数组值。数组从零开始,0 是第一个元素,1 是第二个元素,依此类推。

与您的代码等效的是:

var arr = document.cookie.split("=");
userName = arr[1];

这用等号 (=) 分隔 document.cookie 并从中获取第二个元素(索引 1)。 document.cookiedocument 对象的一个​​特殊属性(数据类型:字符串),它包含网页的所有 cookie,由 ; 字符分隔。例如,如果 document.cookie 包含 name=Adam,则数组 arr 将包含值 nameAdam。第二个存储在 userName 中。

请注意,如果 cookie 包含多个值,或者值包含多个等号,则它将不起作用。考虑以下情况:

  • document.cookie contains name=Adam; home=无处。使用上面的代码,这将使 userName 包含 Adam; home 因为字符串是用等号分隔的,然后取第二个值。
  • document.cookie 包含 home=Nowhere;名称=亚当。这将导致 userName 包含 Nowhere; name
  • document.cookie 包含 name=Adam=cool。在本例中,userName 将为 Adam 而不是 Adam=cool

另外,w3schools 不太可靠。使用更权威的来源,例如 Mozilla 开发者网络:

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:

var arr = document.cookie.split("=");
userName = arr[1];

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 the document object which contains all cookies of a webpage, separated by the ; character. E.g. if document.cookie contains name=Adam, the array arr will contain the values name and Adam. The second one is stored in userName.

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 contains name=Adam; home=Nowhere. Using the above code, this would make userName contain Adam; home because the string is separated by the equal-sign, and then the second value is taken.
  • document.cookie contains home=Nowhere; name=Adam. This would result in userName containing Nowhere; name
  • document.cookie contains name=Adam=cool. In this case, userName would be Adam and not Adam=cool.

Also, w3schools is not that reliable. Use more authorative sources like the Mozilla Developer Network:

眼眸 2024-12-12 17:47:54

split 函数返回按给定分隔符分割的字符串数组。使用方括号,您可以访问该(返回的)数组的第 n 个元素。

如果您熟悉 Java,它的行为与 Java 中的 String.split() 方法相同。

The split function returns an array 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.

暖心男生 2024-12-12 17:47:54

它获取结果数组的第二个索引

相同:

var split = document.cookie.split("=");
var userName = split[1];

It gets the second index of the resulting array

Same as:

var split = document.cookie.split("=");
var userName = split[1];
临风闻羌笛 2024-12-12 17:47:54

split 返回一个字符串数组。所以方括号意味着从返回的数组中获取第二个字符串。

split returns an array of strings. So square brackets mean get second string from the returned array.

童话里做英雄 2024-12-12 17:47:54

您提供的代码中的方括号用于访问 split() 返回的数组的第二个元素。该函数本身返回一个数组。该代码与以下内容相同:

var temp = document.cookie.split("=");
userName = temp[1];

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:

var temp = document.cookie.split("=");
userName = temp[1];
有木有妳兜一样 2024-12-12 17:47:54

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

甜点 2024-12-12 17:47:54

不应该使用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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文