In JavaScript, not everything is an object.

Source: Internet
Author: User

Although many languages claim that "Everything is an object", not all values in javascript are objects.

Original Value vs object
Values in javascript can be divided into two categories: original values and objects ).

Definition
The two types of javascript values are defined as follows:

The following values are original values.

1. String
2. Number: All numbers in JavaScript are floating-point numbers.
3. Boolean Value
4. null
5. undefined


All other values are objects ).Objects can be further divided:

1. Package of the original value: Boolean, Number, String. Rarely used directly.

2. Create an object literally. You can create an object by using the constructor. You can use the literal to create an object.

• [] Is new Array ()
• {} Is new Object ()
• Function () {} is new Function ()
•/\ S */is new RegExp ("\ s *")
3. Date: new Date ("2011-12-24 ")

Differences
You can use the enumerated primitive and the Defined Object non-primitive definition primitive and object. But you can also describe the primitive and object. Let's start with the object.

1. The object is variable:

Copy codeThe Code is as follows:
> Var obj = {};
> Obj. foo = 123; // Add attributes and values
123
> Obj. foo // read attribute, return the attribute value
123

2. Each object has its own unique identifier. Therefore, the objects created through the literal or constructor are not the same as any other objects. We can compare them through ====.

Copy codeThe Code is as follows:
>{}== {}
False

Objects are compared by reference. Only two objects with the same ID are considered equal.

Copy codeThe Code is as follows:
> Var obj = {};
> Obj = obj
True

3. Variables store object references. Therefore, if the two variables apply the same object-when we change one of the variables, the two variables change accordingly.

Copy codeThe Code is as follows:
> Var var1 = {};
> Var var2 = var1;

> Var1.foo = 123; // modify the attribute of the variable val1
123
> Var2.foo // val2 also changed
123

As expected, the original value is different from the object:

1. The original values are unchangeable. You cannot add attributes to them:

Copy codeThe Code is as follows:
> Var str = "abc ";
> Str. foo = 123; // Add attributes (this operation will be ignored)
123
> Str. foo // The value of the read attribute. undefined is returned.
Undefined

2. the original value does not have an internal identifier, and the original value is compared by value: the basis for comparing two original values is their content. If the content of the two original values is the same, the two original values are the same.

Copy codeThe Code is as follows:
> "Abc" = "abc"
True

This means that the identifier of an original value is its value, and the javascript engine does not assign a unique identifier to the original value.

The combination of the last two facts means that we cannot distinguish whether a variable is an object reference or a copy of the original value.

Trap: original values and their packaging types
Rule: Ignore as many packaging types as possible. In other programming languages such as Java, you seldom notice them.

The original value types 'boolean', 'number', and 'string' have their own packaging types 'boolean', 'number', and 'string. The instances of the packaging type are all object values, and the conversion between the two types is also very simple:

• Conversion to packaging type: new String ("abc ")
• Convert to the original type: new String ("abc"). valueOf ()
There are many differences between the original value types and their corresponding Package Types, such:

Copy codeThe Code is as follows:
> Typeof "abc"
'String'
> Typeof new String ("abc ")
'Object'

> "Abc" instanceof String
False
> New String ("abc") instanceof String
True

> "Abc" = new String ("abc ")
False

An instance of the packaging type is an object. Therefore, like JavaScript and objects, the packaging type cannot be compared with values (only reference can be compared ).

Copy codeThe Code is as follows:
> Var a = new String ("abc ");
> Var B = new String ("abc ");
> A = B
False // although a and B share the same content, false is returned.
> A =
True

The original value does not have its own method.
Packaging object types are rarely used directly, but their prototype objects define many methods that can be called with their corresponding original values. For example, String. prototype is the prototype of the String packaging type. All its methods can be used on the original string value. The String. prototype. indexOf method of the packaging type also exists on the original value of the String. They are not two methods with the same name, but are indeed the same method:

Copy codeThe Code is as follows:
> "Abc". charAt === String. prototype. charAt
True

The prototype of the Number packaging type has the toFixed method, namely Number. prototype. toFixed. But an error occurs when we write the following code:

Copy codeThe Code is as follows:
> 5. toFixed (3)
SyntaxError: Unexpected token ILLEGAL

This error is a parsing error (SyntaxError), followed by a dot (.) After 5, which is treated as a decimal point, and should be followed by a number after the decimal point. The following code can run properly:

Copy codeThe Code is as follows:
> (5). toFixed (3)
"5.000"
> 5. toFixed (3)
"5.000"

Classification of values: typeof and instanceof
If you want to classify values, pay attention to the difference between the original value and the object. The typeof operation can be used to distinguish between the original value and the object. Instanceof can be used to differentiate objects. In addition, instanceof returns false for all original values.

Typeof
Typeof can be used to determine the type of the original value and distinguish between the object Value and the original value:

Copy codeThe Code is as follows:
> Typeof "abc"
'String'
> Typeof 123
'Number'
> Typeof {}
'Object'
> Typeof []
'Object'

Typeof returns the following string:

Parameters Result
Undefined "Undefined"
Null "Object"
Boolean Value "Boolean"
Number "Number"
String "String"
Function "Function"
Others "Object"

Note:

• Typeof will return "object" when the operation is null, which is a bug in the JavaScript language. Unfortunately, this bug will never be fixed, because too many existing code already depends on this performance. This does not mean that null is actually an object [4].

• Typeof also allows you to check whether a variable has been declared without throwing an exception. No function can implement this function, because you cannot pass an undeclared variable to a function parameter.

Copy codeThe Code is as follows:
> Typeof undeclaredVariable
'Undefined'
> UndeclaredVariable
ReferenceError: undeclaredVariable is not defined

• Functions are also object types, which may not be understood by many people, but sometimes they are very useful.

• An array is an object.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.