[size=medium]Numbers and booleans are primitive types in JavaScriptprimitive because they consist of nothing more than a small, fixed number of bytes that are easily manipulated at the low levels of the JavaScript interpreter. Objects, on the other hand, are
reference types. Arrays and functions, which are specialized types of objects, are therefore also reference
types. These datatypes can contain arbitrary numbers of properties or elements, so they cannot be manipulated as easily as fixed-size primitive values can. Since object and array values can become quite
large, it doesn't make sense to manipulate these types by value, because this could involve the inefficient
copying and comparing of large amounts of memory.
What about strings? A string can have an arbitrary length, so it would seem that strings should be
reference types. In fact, though, they are usually considered primitive types in JavaScript simply because
they are not objects. Strings don't actually fit into the primitive-versus-reference type dichotomy.
JavaScript strings are (presumably) copied and passed by reference, they are compared by value.
Numbers, boolean values, and the null and undefined types are primitive. Objects,
arrays, and functions are reference types.
[/size]
reference types. Arrays and functions, which are specialized types of objects, are therefore also reference
types. These datatypes can contain arbitrary numbers of properties or elements, so they cannot be manipulated as easily as fixed-size primitive values can. Since object and array values can become quite
large, it doesn't make sense to manipulate these types by value, because this could involve the inefficient
copying and comparing of large amounts of memory.
What about strings? A string can have an arbitrary length, so it would seem that strings should be
reference types. In fact, though, they are usually considered primitive types in JavaScript simply because
they are not objects. Strings don't actually fit into the primitive-versus-reference type dichotomy.
JavaScript strings are (presumably) copied and passed by reference, they are compared by value.
Numbers, boolean values, and the null and undefined types are primitive. Objects,
arrays, and functions are reference types.
[/size]
本文深入探讨了JavaScript中的数据类型,区分了原始类型如数字、布尔值与引用类型如对象、数组和函数。此外还讨论了字符串的独特地位及其操作方式。
585

被折叠的 条评论
为什么被折叠?



