File tree 1 file changed +46
-0
lines changed
1 file changed +46
-0
lines changed Original file line number Diff line number Diff line change
1
+ ---
2
+ chapter : js101
3
+ section : 7
4
+ title : Objects
5
+ attribution : jQuery Fundamentals
6
+ ---
7
+
8
+ Objects contain one or more key-value pairs. The key portion can be any string.
9
+ The value portion can be any type of value: a number, a string, an array, a
10
+ function, or even another object. When one of these values is a function, it’s
11
+ called a method of the object. Otherwise, they are called properties.
12
+
13
+ As it turns out, nearly everything in JavaScript is an object -- arrays,
14
+ functions, numbers, even strings -- and they all have properties and methods.
15
+
16
+ <div class =" example " markdown =" 1 " >
17
+ Creating an "object literal"
18
+
19
+ var myObject = {
20
+ sayHello : function() {
21
+ console.log('hello');
22
+ },
23
+
24
+ myName : 'Rebecca'
25
+ };
26
+
27
+ myObject.sayHello(); // logs 'hello'
28
+ console.log(myObject.myName); // logs 'Rebecca'
29
+ </div >
30
+
31
+ <div class =" note " markdown =" 1 " >
32
+ ## Note
33
+
34
+ When creating object literals, you should note that the key portion of each
35
+ key-value pair can be written as any valid JavaScript identifier, a string
36
+ (wrapped in quotes) or a number:
37
+
38
+ var myObject = {
39
+ validIdentifier : 123,
40
+ 'some string' : 456,
41
+ 99999 : 789
42
+ };
43
+
44
+ Object literals can be extremely useful for code organization; for more
45
+ information, read Using Objects to Organize Your Code by Rebecca Murphey.
46
+ </div >
You can’t perform that action at this time.
0 commit comments