1. Factory Function
A Factory function is any function that returns a new object for every function call.
The Function name should follow the camelCase naming convention.
Syntax:
1.1 Shorthand Notations
2. Constructor Function
A regular function that returns a new object on calling with the
The Function name should follow the PascalCase naming convention.
Syntax:
2.1 The new Operator
When a function is called with the
- Creates an empty object and assigns it to this
- Return this
Here,
- car1is instance
- car1.start()is instance method
- car1.color,car1.brandare instance properties
2.2 Factory vs Constructor Functions
Factory Functions | Constructor Functions |
---|---|
Follows camelCase notation | Follows PascalCase notation |
Doesn't need new operator for function calling | Needs new operator for function calling |
Explicitly need to return the object | Created object returns implicitly |
Try out the Factory and Constructor Functions in the JavaScript Code Playground.
3. JS Functions
Similar to Objects, Functions also have properties and methods.
3.1 Default Properties
- name
- length
- constructor
- prototype, etc.
3.2 Default Methods
- apply()
- bind()
- call()
- toString(), etc.
3.3 Function Properties
3.3.1 The name Property
This property returns the name of the function.
3.3.2 The length Property
This property returns the number of parameters passed to the function.
3.3.3 The typeof function
4. The Constructor Property
Every object in JavaScript has a constructor property.
The constructor property refers to the constructor function that is used to create the object.
5. Built-in Constructor Function
These are the Constructor functions provided by JavaScript.
- function Date()
- function Error()
- function Promise()
- function Object()
- function String()
- function Number(), etc.
In JavaScript, date and time are represented by the Date object. The Date object provides the date and time information and also provides various methods.
5.1 Creating Date Objects
There are four ways to create a date object.
- new Date()
- new Date(milliseconds)
- new Date(datestring)
- new Date(year, month, day, hours, minutes, seconds, milliseconds)
5.1.1 new Date()
You can create a date object without passing any arguments to the
For example,
Here,
- Coordinated Universal Time (UTC) - It is the global standard time defined by the World Time Standard. (This time is historically known as Greenwich Mean Time, as UTC lies along the meridian that includes London and nearby Greenwich in the United Kingdom.)
- Local Time - The user's device provides the local time.
5.1.2 new Date(milliseconds)
The
The
For example,
5.1.3 new Date(date string)
The
Syntax:
You can also pass only the year and month or only the year. For example,
Short date format
Long date format
Month and Day can be in any order
The month can be full or abbreviated. Also, month names are case insensitive.
5.1.4 new Date(year, month, day, hours, minutes, seconds, milliseconds)
It creates a new date object by passing a specific date and time.
For example,
Here, months are counted from 0 to 11. January is 0 and December is 11.
The passed argument has a specific order.
If four numbers are passed, it represents the year, month, day and hours.
For example,
Similarly, if two arguments are passed, it represents year and month.
For example,
5.2 AutoCorrection in Date Object
When you assign out of range values in the Date object, it auto-corrects itself.
For example,
33 days are auto corrected to 31 (jan) + 2 days in feb.
5.3 Instance Methods
There are methods to access and set values like a year, month, etc. in the Date Object.
Method | Description |
---|---|
now() | Returns the numeric value corresponding to the current time (the number of milliseconds passed since January 1, 1970, 00:00:00 UTC) |
getFullYear() | Gets the year according to local time |
getMonth() | Gets the month, from 0 to 11 according to local time |
getDate() | Gets the day of the month (1–31) according to local time |
getDay() | Gets the day of the week (0-6) according to local time |
getHours() | Gets the hour from 0 to 23 according to local time |
getMinutes | Gets the minute from 0 to 59 according to local time |
getUTCDate() | Gets the day of the month (1–31) according to universal time |
setFullYear() | Sets the full year according to local time |
setMonth() | Sets the month according to local time |
setDate() | Sets the day of the month according to local time |
setUTCDate() | Sets the day of the month according to universal time |
5.3.1 Setting Date Values
Try out the JS function properties and Date Methods in the JavaScript Code Playground.