In JavaScript not all of everything is Object _ basics

Source: Internet
Author: User
Tags parse error wrapper

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

Original Value vs Object
The values in JavaScript can be divided into two broad categories: raw values (primitive) and objects (object).

Defined
The definition of two values for javascript:

The following value is the original value.

1. String
2. Number: All numbers in JavaScript are floating points
3. Boolean value
4.null
5.undefined


all other values are objects (object). objects can be further divided:

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

2. An object created with a literal amount. The following literal produces an object, or you can create an object from a constructor. You can create objects using literal amounts.

[] is the new Array ()
{} is the new Object ()
function () {} is the new function ()
/\s*/is the new RegExp ("\\s*")
3. Date: New Date ("2011-12-24")

Difference
You can define primitives and objects by enumerating the primitives and defining objects in a primitive language. But you can also describe what the primitives and objects are. Let's start with the object.

1. The object is variable:

Copy Code code as follows:

> var obj = {};
> Obj.foo = 123; Adding Properties and values
123
> Obj.foo/Read property, returns the value of the property
123

2. Each object has its own unique identifier, so the object created by the literal or constructor is not equal to any other object, and we can compare it by = =.

Copy Code code as follows:

> {} = = = {}
False

objects are compared by reference, and only two objects have the same identity to consider the object to be equal.

Copy Code code as follows:

> var obj = {};
> obj = = obj
True

3. The variable holds the reference to the object, so if two variables apply the same object-we change one of the variables, the 21 will change as well.

Copy Code code as follows:

> var var1 = {};
> var var2 = var1;

> Var1.foo = 123; Modifying the properties of a variable val1
123
> Var2.foo//VAL2 also changed
123

As expected, the original values and objects are different:

1. The original value is immutable; you can't add attributes to them:

Copy Code code as follows:

> var str = "ABC";
> Str.foo = 123; Add attribute (This action will be ignored)
123
> Str.foo/Read property value, return undefined
Undefined

2. The original values are not internally identifiable and the original values are compared by value: the basis for comparing two original values is their content, and if the contents of two original values are the same, the two original values are considered identical.

Copy Code code as follows:

> "abc" = = "abc"
True

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

The last two facts combine to mean that we cannot tell whether a variable is a reference to an object or a copy of the original value.

Traps: Original values and their wrapper types
Rule: Ignore as many wrapper types as possible. In other programming languages such as Java, you rarely notice them.

The original value type Boolean, number, and string all have their own corresponding wrapper type Boolean, number, and string. An instance of a wrapper type is an object value, and the conversion between the two types is simple:

• Convert to wrapper type: New String ("abc")
• Convert to original type: New String ("abc"). valueof ()
The original value types and their corresponding wrapper types have many different points, such as:

Copy Code code 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 a wrapper type is an object, and therefore, like JavaScript and objects, the wrapper type cannot be compared to a value (you can only compare references).

Copy Code code as follows:

> var a = new String ("abc");
> var b = new String ("abc");
> A = = b
False//Although A and B have the same content, but still return false
> A = = a
True

The original value does not have its own method
Wrapper object types are rarely used directly, but their prototype objects define a number of methods whose corresponding original values can also be invoked. For example, String.prototype is a prototype object of the wrapper type String. All of its methods can be used on the original value of the string. The method of wrapping type is String.prototype.indexOf on the original value of the string, they are not two methods with the same name, and they are indeed the same method:

Copy Code code as follows:

> "abc". CharAt = = String.prototype.charAt
True

The prototype object for the number of the wrapper type numbers has a toFixed method, that is, Number.prototype.toFixed, but an error occurs when we write the following code:

Copy Code code as follows:

> 5.toFixed (3)
syntaxerror:unexpected token illegal

This error is a parse error (SYNTAXERROR), followed by a dot (.), 5, which is treated as a decimal point, and after the decimal point should be a number, the following code can work correctly:

Copy Code code as follows:

> (5). toFixed (3)
"5.000"
> 5..toFixed (3)
"5.000"

Classification of values: typeof and Instanceof
If you want to classify values, you need to be aware of the difference between the original value and the object. typeof operations can be used to differentiate between original values and objects. Instanceof can be used to differentiate objects, and instanceof returns false for all original values.

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

Copy Code code as follows:

> typeof "ABC"
' String '
> typeof 123
' Number '
> typeof {}
' Object '
> typeof []
' Object '

typeof returns the following string:

Parameters Results
Undefined "Undefined"
Null "Object"
Boolean value "Boolean"
Digital "Number"
String "String"
Function "Function"
Other "Object"

Comments:

typeof returns "Object" when manipulating null, which is a bug in the JavaScript language itself. Unfortunately, this bug can never be fixed, because too many existing code already relies on this performance. This does not mean that NULL is actually an object [4].

typeof also lets you check whether a variable is declared, without throwing an exception. No single function can implement this function, because you cannot pass an undeclared variable to a function's arguments.

Copy Code code as follows:

> typeof undeclaredvariable
' Undefined '
> undeclaredvariable
Referenceerror:undeclaredvariable is not defined

• Functions are also object types; This may be incomprehensible to many people, but it can be very useful sometimes.

• An array is an object.

More information about TypeOf [5] and [6].

instanceof
Instanceof can detect whether a value is an instance of a constructor:

Copy Code code as follows:

Value Instanceof Constructor

If the expression above returns True, the value is an instance of constructor. It is equivalent to:

Copy Code code as follows:

Constructor.prototype.isPrototypeOf (value)

Most objects are instances of object because the end of the prototype chain (prototype chain) is Object.prototype. The original value is not an instance of any object:

Copy Code code as follows:

> "abc" instanceof Object
False
> "abc" instanceof String
False

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.