Classes and objects
#
1. How do you define a class in JavaScript?A class is a template for building objects. Objects are variables that can contain many values (properties)
Class syntax:
- Classes' name begin with capital letter as a convention.
- The object
constructor
method is a special method of a class for creating and initializing an object of that class. A constructor enables you to provide any custom initialization that must be done before any other methods can be called on an instantiated object.
#
2. What is a class property?Class' properties are what build an object. Object is a collection of unordered properties.
- To access an object's property inside the class we use the reserved word
this.
- To access an object's property outside the class we use
objectName.propertyName
.
#
3. What does mean to instantiate an object of a class?To instantiate an object is to create a new object from a class.
- We define properties giving a value for each of them, separating values with comas. They can be strings, numbers, etc. It depends on your object's properties.
#
4. What is a class method and how do you use it ? Can methods in classes access to class properties? How?- Methods are another class members, such as the constructor (that is a special method).
- Class methods are created with the same syntax as object methods. You can add any number of methods.
- Class methods are used as functions related with its class properties defined in the constructor. We also can access the function from each new object. The results will be based on the object being used.
Example:
#
5. What is the difference between a standard class method and a static method?The
static
keyword defines a static method or property for a class. Astatic
method is that method that can be ran without having to instantiate a class.
- Static class methods are defined on the class itself. You cannot call a static method on an object, only on an object class. In other words,
static
methods have no access to data stored in specific objects. - Static methods are often used to create utility functions for an application, whereas static properties are useful for caches, fixed-configuration, or any other data you don't need to be replicated across instances.
Example:
Setters & getters
Setters (
set
) and getters (get
) are special methods that allow us define and obtain values from our class' properties.
- They behave as they were methods.
get
is used to obtain (return
) the value of a property.set
is used to asign new values for your properties.
Setter example:
Getter example: If we set a new value value for the area (ex:25) and use a setter method, we will redefine each property into 5.
#
6. How do you define a subclass in JavaScript?
extends
keyword is used in class declarations to create a class that is a child of another class.
Syntax:
class ChildClass extends ParentClass { ... }
The child class inherits all the methods from another class.
Inheritance is useful for code reusability: reuse properties and methods of an existing class when you create a new class.
Note: the parent class cannot inherit methods from the extended class.
We use the
super()
method to refer to the parent class. By calling thesuper()
method in the constructor method, we call the parent's constructor method and gets access to the parent's properties and methods. Then we can add properties into the constructor.
#
8. General info about how to use classes in JavaScript๐ Mozilla: Mozilla classes
๐ Code Academy Lessons 8 & 9: Lesson 8: objects & Lesson 9: classes
๐ Objects cheatsheet
๐ Classes cheatsheet
#
9. Crash course about classes in JavaScriptVideo tutorials in spanish:
๐ Bluuweb! Basic tutorial: Youtube tutorial
๐ Dcode tutorial: Youtube tutorial
#
10. A GitHub repository/page with exercises with classes (better if they have solution)https://github.com/LambdaSchool/JS-Exercise-Classes point_right: GitHub repository: JS-Exercise-Classes Complete the tasks on index.html