What is Object?
JavaScript is an object-based programming language and almost “everything” that makes up JavaScript is an object. The rest of the values (functions, arrays, regular expressions, etc.) are all objects except primitive types.
Object / reference type is a complex data structure that consists of several types of values (primitive type values or other objects) in a single unit.
Primitive value | Object |
---|---|
immutable value | mutable value |
pass by value | pass by reference |
Object is a set of properties that consist of keys and values.
Any value that is available in JavaScript can be used as the value of the property. Functions in JavaScript are first-class objects, so they can be treated as values.
JavaScript functions are first-class objects, so they can be used as property values. If the property value is a function, it is called a method to distinguish it from a normal function.
- Attributes are refer to additional information of an object.
- Properties are describing the characteristics of an object.
An object is a set of methods, which means properties that refer to data and behavior that can refer to and manipulate data.
Create Object by Object Literal
Class-based object-oriented languages such as C ++ and Java pre-define classes and create objects by calling the constructor with the new operator at the point in time. However, JavaScript is a prototype-based object-oriented language that creates objects differently.
Then how JavaScript creates object?
- Object literal
- Object constructor function
- Constructor function
- Object.create method
- Class (ES6)
The most basic and general method is using object literal.
1 | // empty object |
The {}
used to create the object is not a block of code. It is an object literal because it ends with a ;
.
You can create a property as soon as you create the object by including the property in an object literal, or you can dynamically add a property after you create the object.
Property
An object is a set of properties. A property consists of a key and a value.
The values that can be used for property key and property values are as follows.
- Property key : all strings include empty string or symbol value
- Property value : all values in JavaScript
Quotes can be omitted if the property key is a name that conforms to the identifier naming convention, that is, a valid name that can be used in JavaScript.
1 | var student = { |
Property keys are allowed to be duplicated, and in the event of duplication, the last declared key is valid.
Method
If the property value is a function, it is called a method to distinguish it from a normal function. In other words, a method means a function restricted to an object.
1 | var circle = { |
this
is a reference variable that points to the object that called it.
Access of Property
You can access the properties in such a way,
- Dot notation
- Bracket notation
1 | var student = { |
If the property key follows the identifier naming convention, 2 notations are both available. If not, only bracket notation is available.
If the property key is not a name that does conform to the identifier naming convention, that is, not a valid name that can be used in JavaScript, must use square bracket notation. However, the quotation marks ''
can be omitted if the property key is a string of numbers.
Update Property Value
If assign a value to an existing property, the property value is updated.
1 | var student = { |
Dynamic property creation
1 | var student = { |
Can add, modify, and delete properties after object creation is complete.
New ES6 Object Literal Functions
Property shorthand
1 | let x = 1, y = 2; |
Property key dynamic creation
1 | // ES6 |
Method shorthand
1 | // ES6 |