JS Classes
- In JavaScript, classes were introduced in ECMAScript 2015 (ES6) as a new way of defining objects and object-oriented programming.
- A class in JavaScript is a blueprint for creating objects with properties and methods.
- To create a class in JavaScript, the
class
keyword is used and followed by the class name. - In JavaScript, classes can be created using the class keyword. The basic syntax for creating a class in JavaScript is as follows:-
constructor() {
// constructor code
}
// method code
}
// method code
}
}
ClassName
is the name of the class, which can be any valid identifier. The constructor()
method is a special method that is called when an object of the class is created using the new
keyword. It is used to initialize the object’s properties and perform any other setup that needs to be done.JS Object
-
- The object is one of the significant and broad data types of JS which represents instances through which we can access member’s data.
- JavaScript is an object-based scripting language. It allows us to define our own objects and make our own variable types.
- It also offers a set of predefined objects. The tables, forms, buttons, images, or links on our web page are examples of objects.
- JS Object’s Property & Methods:
- JS objects are composed of attributes. When an attribute contains a function, it is considered to be a method of the object, otherwise, the attribute is considered a property.
- There is a small difference between a function and a method i.e. a function is a standalone unit of statements and a method is attached to an object and can be referenced by this keyword.
- The values associated with objects are properties and the actions that can perform on objects are methods or behavior.
- The syntax for adding a property to an object is (i.e. Property associated with an object can be accessed) as follows:-
objectName.object property name = propertyValue;
- To create user-defined objects (not built-in objects) –
- In JavaScript, objects can be created using the object literal syntax, constructor functions, and the
Object.create()
method [Explained in program examples topics].
- In JavaScript, objects can be created using the object literal syntax, constructor functions, and the
-
- Some popular predefined objects in JavaScript are:-
- DOCUMENT OBJECT:-
- The Document object is one of the parts of the Window object.
- It can be accessed through the window—document property.
- The document object represents a HTML document and it allows one to access all the elements in a HTML document.
- For example, the title of the current document can be accessed by “document.title” property.
- Some of the common properties of document objects are :-
- DOCUMENT OBJECT:-
- Some popular predefined objects in JavaScript are:-
Title : returns/ sets the title of the current document.
bgColor : returns/ sets the background color of the recent document.
fgColor : returns/ sets the text color of the recent document.
linkColor : returns/ sets the color of hyperlinks in the document.
alinkColor : returns/ sets the color of active links in the document.
vlinkColor : returns/ sets the color of visited hyperlinks.
height : returns the height of the current document.
width : returns the width of the recent document.
Forms : returns a list of the FORM elements within the current document.
Images : returns a list of the images in the current document.
URL : returns a string containing the URL of the current document.
Location : to load another URL in the current document window
-
-
-
- Some of the common methods of the document object are :-
-
-
open() : Opens a document for writing.
write() : Writes string/data to a document.
writeln() : Writes string/data followed by a newline character to a document.
close() : Closes a document stream for writing.
-
-
-
- Examples of properties and methods of the document object are :-
-
-
<script>
</script>
-
-
- DATE OBJECT :
- Date object is used to set and manipulate date and time.
- JavaScript dates are stored as the number of milliseconds since midnight, January 1, 1970. This date is called the epoch. Dates before 1970 are represented by negative numbers.
- A date object can be created by using the new keyword with Date().
- Syntax
- DATE OBJECT :
-
newDate()
new Date(milliseconds)
new Date(dateString)
new Date(yr_num, mo_num, day_num [,hr_num, min_num, sec_num, ms_num])
dateString means Date String. e.g. “October 5, 2007”
yr_num, mo_num,day_num means Year (e.g. 2007)Month (Value 0-11, 0 for
January and 11 for December), Day (1-31) and
hr_num, min_num,sec_num, ms_num means Values for Hour, Minutes, Second and milliseconds
-
-
-
- Some common methods of Date Object are –
- We can use the get methods to get values from a Date object.
- Here are some common get methods that return some date-related value according to local time :
- Some common methods of Date Object are –
-
-
getDate( ) : Returns the day of the month
getDay( ) : Returns the day of the week
getFullYear( ) : Returns the full year
getHours( ) : Returns the hour
getMinutes( ) : Returns the minutes
getMonth( ) : Returns the month
getSeconds( ) : Returns the seconds
getTime( ) : Returns the numeric value corresponding to the time
getYear( ) : Returns the year
-
-
-
-
Example :
-
-
-
-
-
- MATH OBJECT :
- This object contains methods and constants to carry out more complex mathematical operations.
- This object cannot be instantiated like other objects.
- All properties and methods of Math are static.
- Some common properties of Math objects are –
- MATH OBJECT :
-
Math.PI : Returns the value of pi.
Math.LN2 : Natural logarithm of 2.
Math.LN10 : Natural logarithm of 10, approximately 2.302.
SQRT1_2 : Square root of ½.
SQRT2 : Square root of 2.
-
-
-
- Some common methods of Math objects are –
-
-
pow(x, p) : Returns XP
abs(x) : Returns absolute value of x.
exp(x) : Returns ex
log(x) : Returns the natural logarithm of x.
sqrt(x) : Returns the square root of x.
random() : Returns a random number between 0 and 1.
ceil(x) : Returns the smallest integer greater than or equal to x.
floor(x) : Returns the largest integer less than or equal to x.
min(x, y) : Returns the smaller of x and y.
max(x, y) : Returns the larger of x and y.
round(x) : Rounds x up or down to the nearest integer.
sin(x) : Returns the sin of x, where x is in radians.
-
-
-
-
Example :
-
-
-
0 Comments