0% found this document useful (0 votes)
11 views

01-05 Class and Properties

Uploaded by

tejasbadgujar238
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

01-05 Class and Properties

Uploaded by

tejasbadgujar238
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Basics Of JavaScript Programming

 Class
 Properties
 Methods
 Constructor
 Inheritance
 Getters / Setters

Classe, Objects and Properties.


 In JavaScript, classes are a way to create objects.
 It work with OOP principles like inheritance, encapsulation, and abstraction.
 Key Concepts
o Class: Template for creating objects.
o Properties: Variables attached to the class that hold data.
o Methods: Functions inside a class that operate on class properties.
o Constructor: Special method for initializing properties when an object is created.
o Inheritance: A way to extend a class and reuse its functionality in another class.
o Getters/Setters: Methods to control access to properties.
 Creating a Class
o A class is defined using the class keyword followed by the class name.
o It can contain a constructor method, which is used to initialize object properties,
and other methods that operate on objects.
o Example:
o In The Following Example.
o Person is a class.
o The constructor initializes the name and age properties.
o show() is a method of the class.

Subject: Java Script Notes Print Date: [17/Oct/24], Page 1 of 9


 Properties in JavaScript Classes
o Properties in classes refer to the variables associated with an object, which can
hold values.
o In the example above, name and age are properties of the Person class.
o Properties are usually defined and initialized in the constructor method.
 Methods in Classes
o A method is a function that is associated with a class.
o It can be used to perform actions on object properties or other tasks.
o The show() function is an example of a method in the Person class.
 Inheritance
o Classes can inherit from other classes, allowing you to create more specific
versions of a class.
o Example

Subject: Java Script Notes Print Date: [17/Oct/24], Page 2 of 9


 Getters and Setters
o Getters and setters allow you to define methods that are used to get or set the
values of properties.
o It encapsulate the internal state of object.
o Getters and Setters are typically named without the “get” or “set” prefix.
o They represent a single properties.
o You define a getter or setter with get or set keyword followed by method name,
o When they accessed or assigned, they look like regular properties.
o Getter/Setter method names connot conflict with the actual property names.
o Its common practice to use an underscore (_) before internal property names
like ( _radius ) to distinguish them from the getter / setter method names.

Subject: Java Script Notes Print Date: [17/Oct/24], Page 3 of 9


o A getter function should not take any parameters. It is invoked like a property, so
no argument are expected.
o A setter function must accept exactly one parameter ( the value assigned to the
property)
o Getters/setters behave as if they are normal object properties. This means you
don’t call them as functions but accessed them like regular properties.
o You can not define both a getter and a regular property with the same name.
o You can define either setter or getter or both.
o If you define only a getter, the property will be read only.
o If you define only a setter, the property cannot be read.
o Get method return the value
o Set method assign the value.
o Getter / Setter are design for each property.
o Example:

Subject: Java Script Notes Print Date: [17/Oct/24], Page 4 of 9


Querying and Setting Properties
 In JavaScript, properties of objects can be queried (retrieved) or set (assigned) using
different techniques.
 These properties store values that can be primitive data types (strings, numbers,
booleans) or objects and functions.
 Below are different ways to query and set properties in JavaScript.
 Dot Notation
o Querying (Getting) a Property:
 You can retrieve the value of a property using the dot (.) notation.
o Setting a Property:
 You can also assign a value to a property using the dot notation.
o Example:
Subject: Java Script Notes Print Date: [17/Oct/24], Page 5 of 9
 Bracket Notation
o Querying (Getting) a Property:
 Bracket notation allows you to query a property using a string that represents
the property name.
 This is especially useful when the property name is dynamic or includes
special characters.
o Setting a Property:
 You can also assign a value to a property using the bracket notation.
o Example:

Subject: Java Script Notes Print Date: [17/Oct/24], Page 6 of 9


o When to Use Bracket Notation:
 When property names have special characters or spaces.
 When accessing a property name dynamically.
 Example (Dynamic Property Name):
 var property = 'name';
 document.write(“<br> … “+Person[property]);
o Adding New Properties
 You can add new properties to an object at any time using either dot notation
or bracket notation.
o Deleting Properties
 To remove a property from an object, you can use the delete operator.

Subject: Java Script Notes Print Date: [17/Oct/24], Page 7 of 9


o Checking if a Property Exists
 You can check whether a property exists in an object
 using the in operator or by using the hasOwnProperty() method.

o Enumerating Properties
 You can iterate over the properties of an object using a for...in loop.

Subject: Java Script Notes Print Date: [17/Oct/24], Page 8 of 9


Subject: Java Script Notes Print Date: [17/Oct/24], Page 9 of 9

You might also like