Javascript 中的字符串和字符数组有什么区别? [英] What's the difference between a string and an array of characters in Javascript?

查看:28
本文介绍了Javascript 中的字符串和字符数组有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我检查这两个是否相等时,它们显然不是.有人能解释一下原因吗?

When I checked if these two were equal, they apparently weren't. Can someone explain why?

var string = "Hello";
var array = ['H', 'e', 'l', 'l', 'o'];

为什么 (string === array) 是假的?

这个网站太棒了.这么快的帮助.谢谢各位.

This website is fantastic. Such fast help. Thanks guys.

推荐答案

为什么 (string === array) 是假的?

您正在使用 严格比较 (===),它还检查值的数据类型.显然,原始字符串值与对象的数据类型不同,并且对象只真正等于它们自己.证明:

You are using strict comparison (===), which also checks the data type of the values. Obviously a primitive string value is not the same data type as an object, and objects are only truly equal to themselves. Proof:

var foo = [1,2,3];
var bar = [1,2,3];

console.log(foo === bar); // false
console.log(foo === foo); // true

现在,如果您要使用 <强>松散比较(==),下面的比较确实返回true:

Now, if you were to use loose comparison (==), the following comparison does return true:

console.log([1,2,3] == '1,2,3'); // true

为什么?因为数组首先被转换为字符串,而这恰好导致相同的字符串值.但这并不意味着值相同(一个仍然是数组,另一个是字符串).这就是为什么您应该始终使用严格比较的原因.

Why? Because the array is converted to a string first, and this happens to result in the same string value. But that doesn't mean that the values are the same (one is still an array and the other a string). That's why you should always use strict comparison.

Javascript 中的字符串和字符数组有什么区别?

What's the difference between a string and an array of characters in Javascript?

字符串不是数组,因为它们继承自不同 prototypes (*) 因此有不同的实例方法.例如,数组有一个 方法 join 和字符串有一个 method match.

Strings are not arrays because they are inheriting from different prototypes (*) and hence have different instance methods. For example, arrays have a method join and strings have a method match.

从一个角度来看,数组和字符串相似,因为它们都是类数组对象.

From one point of view, arrays and strings are similar though, because they are both array-like objects.

类似数组是什么意思?这意味着该对象具有 length 属性和数字属性.字符串有一个 length 属性,它为您提供字符串中的字符数,您可以使用 str[i] 访问字符串的单个字符.示例:

What does array-like mean? It means that the object has a length property and numeric properties. A string has a length property which gives you the number of characters in the string, and you can access single characters of the string with str[i]. Example:

var arr = ['a','b','c'];
var str = "abc";

console.log(arr.length); // 3
console.log(str.length); // 3

console.log(arr[0]); // a
console.log(str[0]); // a

console.log(arr === str); // false

console.log(typeof str); // string
console.log(typeof arr); // object

<小时>

*:其实,原始字符串和String对象甚至是有区别的,但我不想在这里讲得太深.从技术上讲,原始字符串没有任何方法,因为它们不是对象,但在大多数情况下,您可以将原始字符串视为对象.


*: Actually, there even is a difference between primitive strings and String objects, but I don't want to go too deep here. Technically primitive strings don't have any methods because they are not objects, but in most cases, you can treat primitive strings like they were objects.

这篇关于Javascript 中的字符串和字符数组有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆