Topics: The this Keyword Arrow Functions and this, Privacy, Getters, Setters, Factory Functions, Property Value Shorthand, Destructured Assignment, Built-in Object Methods
- Create a html file with your name like john.html
- Add current code to that file:
<!DOCTYPE html>
<html>
<body>
<script>
// your code here
</script>
</body>
</html>-
Create a student object with id, firstName, lastName, major, _gpa fields and a method show that show student info like this:
Name: {firstName lastName} (ID:{id} ) Major: {major} GPA: {gpa}Note: Usethiskeyword to access fields in show method. -
Define a getter method named fullName for student. If first name is empty return Mr/Ms {last name} else return {firstName} {lastName}.
-
Define a setter method named gpa. If gpa is less than 0 or greather than 4 , log error else put it into _gpa field.
-
Create a factory function named courseFactory. Use arrow function and pass parameters: id, name, credit. It should return an object with these field and method named show to log all field into console.
-
Call this function with parameters (1, "Math", 3, ) and assign to
myCoursevariable. Show call myCourse.show() and check the result. -
Check that
myCoursehasteacherproperty, if it has log teacher else log no teacher. -
Create another object named
courseAdditionand set it to{teacher: "Dr Mark"} -
Use object.assign to merge courseAddition into myCourse.
-
Check that
myCoursehasteacherproperty, if it has log teacher else log no teacher. -
Show all fields and values into
myCoursewith object.keys
Send Pull Request. Check how to deliver your code: https://codingwithbasir.com/how-to-deliver-projects/
Download Learn JavaScript eBook from https://codingwithbasir.com/learn-javascript
