-
Notifications
You must be signed in to change notification settings - Fork 0
/
objetos.txt
104 lines (76 loc) · 2.38 KB
/
objetos.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
NOTES
=====
**Objects**
Objects can be compared to real world objects (like a car, a spoon, a house,
etc.. ) which have properties and a particular type. In Javascript (and
other programming languages), an object is one of the complex data types,
which have a list of keys and values:
```
const car = {
Model : 'Honda City',
color: 'Red',
owner: 'X1',
yearOfManufacture: 2017
};
```
In the above example each item in the list is a property (e.g: Model, color,
owner, year) of the object 'car'. The object can also have functions called as methods.
The property name/key can be a string or a number.
```
const age = {
10: 'kids',
30: 'smart and wise',
100: 'very very experienced'
};
```
We use objects mostly to store data and for creating custom methods and
functions. There are 2 ways we can create objects:
1. Object Literals
2. Object Constructors
Via Object Literals:
We just declare an object name and within {} define all the properties
with its values:
```
const myNewEmptyObject = {}
const book = {
name: 'Harry Potter Book1',
author: 'J.K. Rowling',
blurb: 'something magical... '
};
sayHello: function() {
console.log('Hello There');
};
```
Via Object Constructors:
Constructors are functions that are used for initialising new objects using
the `new()` keyword. We can set the properties via the `object.propertyname` notation:
```
const book = new Object();
book.name = 'Harry Potter Book1';
book.author = 'J.K.Rowling';
```
Accessing properties of an object:
- Dot notation
- Bracket notation
```
const book = {
name: 'Harry Potter Book1',
author: 'J.K. Rowling',
blurb: 'something magical... '
};
```
Dot notation is the most common way we access the objects. Most of the
examples above use the dot notation. It follows the object-name.key-name syntax:
```
console.log(book.name);
console.log(book.author);
```
Bracket notation follows the object-name[key-name] format:
```
console.log(book['name']);
console.log(book['author']);
console.log(age[10]);
```
Note: the property which is a number must use the bracket notation only.
You can delete the properties of an object using the `delete` keyword:
`delete book.blurb`