Main content
Resources
Course: Resources > Unit 1
Lesson 4: Computer programming teachers- Programming content overview
- Tracking progress of programming students
- Classroom debugging guide
- Pair programming in the classroom
- Teaching guide: Intro to JS - Drawing Basics
- Teaching guide: Intro to JS - Coloring
- Teaching guide: Intro to JS - Variables
- Teaching guide: Intro to JS - Animation basics
- Teaching guide: Intro to JS - Interactive Programs
- Teaching guide: Intro to JS - Resizing with variable expressions
- Teaching guide: Intro to JS - Text and strings
- Teaching guide: Intro to JS - Functions
- Teaching guide: Intro to JS - Logic and if statements
- Teaching guide: Intro to JS - Looping
- Teaching guide: Intro to JS - Arrays
- Teaching guide: Intro to JS - Objects
- Teaching guide: Intro to JS - Object-oriented design
- Programming classroom handouts
- Additional programming projects
- Lesson plans: teaching programming in the classroom
- Programming case study: Encouraging cross-disciplinary projects
- Programming case study: Going beyond the KA curriculum
- Programming case study: Teaching an elementary school class
© 2023 Khan AcademyTerms of usePrivacy PolicyCookie Notice
Teaching guide: Intro to JS - Object-oriented design
This is a teaching guide for the Intro to JS lesson on Object-oriented design.
What the student will learn
- How to define an object using the JavaScript prototype system, so that they can then create multiple instances of that object in their program.
- How to define methods on objects, so that all instances share the same methods.
- How to define an object that inherits from another object, so that it can reuse parent methods but also its own new methods.
The student will be able to write code like:
Where students struggle
- Most students will have some difficulty grasping both the concepts and syntax of object-oriented programming in JavaScript. Object-oriented programming is a technique used across many programming languages, and it’s particularly tricky in JavaScript.
- Students may wonder why object-oriented programming is so important and whether it's worth the difficulty in learning it. OO is a technique that gets more important as programs get bigger and more complex, and it’s particularly useful for the types of programs that are popular on Khan Academy - like games and natural simulations. Those programs all involve objects conceptually - like obstacles and avatars in games, or particles and emitters in simulations. It’s possible to write the programs using just functions, but it’s often easier to write them using object-oriented code, because then you can easily re-use similar functionality in similar objects.
- Students often find the
this
keyword confusing. That’s the keyword that an object used to refer to itself - in other languages, it’s actuallyself
orme
. It’s necessary to use this to tell an object to find a variable that’s a property on itself, instead of a local or global variable. - Students get confused between the object definition and object instances. One thing that helps is if they pay attention to capitalization, as we use that to distinguish. We use capital letters to define an object type, like
Winston
, and lowercase when we create instances, likewinstonTeen
andwinstonAdult
. Remind them that the first step is to define the object type (Winston
) and only then can they create new instances based on that type (winstonTeen
,winstonAdult
). - Students might accidentally define object methods before their object constructor. The object constructor must be first, and then the methods.
- Students may have a hard time understanding the way that object inheritance works in JavaScript, even if they’re able to type the lines of code that makes it work. It helps to really talk through each line of code, and to read the alternative explanations from answers under the inheritance talk-through.
Additional materials: Discussion questions
These are questions that you can ask students individually after they've done the lesson, or lead a group discussion around, if everyone's gotten to the same point.
- Discuss how you would program the concept of animals using OO programming. What properties and methods would you define on an Animal object? What objects would inherit from Animal, and what properties and methods would they have? You can have that same discussion about other objects as well, like Cars, Trees, Books, Shapes. There's no right answer, as the actual way you'd code it would depend on the app/game, but it's a useful exercise in thinking about object attributes, behavior, and inheritance.
Want to join the conversation?
- khan academy is so good(4 votes)
- how do u do this crazy stuff(3 votes)
- If I'm working on visual studios app witch file do I open in visual studio(0 votes)