or
or
By clicking below, you agree to our terms of service.
New to HackMD? Sign up
Syntax | Example | Reference | |
---|---|---|---|
# Header | Header | 基本排版 | |
- Unordered List |
|
||
1. Ordered List |
|
||
- [ ] Todo List |
|
||
> Blockquote | Blockquote |
||
**Bold font** | Bold font | ||
*Italics font* | Italics font | ||
~~Strikethrough~~ | |||
19^th^ | 19th | ||
H~2~O | H2O | ||
++Inserted text++ | Inserted text | ||
==Marked text== | Marked text | ||
[link text](https:// "title") | Link | ||
 | Image | ||
`Code` | Code |
在筆記中貼入程式碼 | |
```javascript var i = 0; ``` |
|
||
:smile: | ![]() |
Emoji list | |
{%youtube youtube_id %} | Externals | ||
$L^aT_eX$ | LaTeX | ||
:::info This is a alert area. ::: |
This is a alert area. |
On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?
Please give us some advice and help us improve HackMD.
Do you want to remove this version name and description?
Syncing
xxxxxxxxxx
Objects
Objects group together related data and/or functionality. We refer to an object's data, we usually mean its properties, and when we refer to its functionality, we usually mean its methods.
In JavaScript, objects are essentially the same as what an object is in real life. A car might have 4 wheels and 3 doors, and it can accelerate, brake etc. They are made up of key/value pairs, so you might have a key of
wheels
with a value of4
.Creating a new object
The most common way to create an object in JavaScript is through the object literal:
You can also do (not recommended):
The benefit of using the object literal (
{}
) is that you can define properties and methods on the object at time of creation:You can also define property/method keys with strings (but it isn't recommended):
Difference between properties and methods
A property is just some data stored on the object:
A method is a function that usually is specific to and/or modifies the object:
You can access properties on the object inside of methods with the
this
keyword.this
refers to the object the method has been called on.Note also that
peel () {}
is shorthand forpeel: function () {}
:this
won't equal the object if you assign an arrow function to the property name, so be careful with your syntax.Accessing properties/methods
You can access an objects properties/methods with dot notation (
.
):You can also use square bracket notation, which is good for when you don't know the property name (it could be a user requesting information on a certain property) or for when/if the property name isn't valid JavaScript:
Square bracket notation converts whatever is passed between the brackets to a string, before trying to find a matching key on the object.
Assigning new properties/methods
You can think as properties/methods on an object as variables that are just attached to the object. Assigning is as easy as variable assignment. You just write the same code you would t access a property/method and follow it by an assignment operator:
If the key (i.e.
wheels
,drive
,email-address
) doesn't exist, then JavaScript will first create it and then assign the value, so objects can be added to.Objects can be nested
Objects can also have other objects nested inside of them as property values:
You've used dot notation and square bracket notation before
Almost everything in JavaScript is an object. This can be seen with arrays for example where you might call
array.push()
(a method) or you might access an array's length (a property) witharray.length
. You can also retrieve an element in an array via square bracket notation (because to start a variable name with a number is illegal in JS):array[0]
.