Giter Site home page Giter Site logo

fewpjs-adding-behavior-with-methods-nyc-web-career-031119's Introduction

Adding Behavior With Methods

Learning Goals

  • Write methods that use instance data and parameter data

Introduction

In review, with Object-Oriented programming (OOP), we can use classes to represent concepts such as students, books, comments, posts, or even animals.

We should only have to define the properties and methods of a class once. Different instances of this class will all have the same properties and methods. Specific values for those properties will be different between instances. Mickey and Minnie are both Mouse instances that have a property called name, but the value of that property for each is different.

With knowledge of constructors, we can use JavaScript's classes as a template for instances.

Write a Method That Uses Instance Data and Parameter Data

To practice OOP concepts, let's create 3 classes that use constructor methods. These constructors will assign properties based on initial parameters. We'll also write methods that leverage these properties.

  1. Create classes Cat, Dog, and Bird
  2. Each of these classes will accept the parameters name and sex and will store those values as properties.
class Cat {
  //...
}

class Dog {
  //...
}

class Bird {
  //...
}

For each class, create the method speak.

  • For an instance of Cat, speak returns "name says meow!",
  • For an instance of Dog, speak returns "name says woof!"
  • For an instance of Bird, speak returns conditional output. If the instance of Bird is male, speak returns "It's me! name, the parrot!". If it is not male, speak returns "name says squawk!".

Conclusion

We've learned to instantiate class instances, or "objects" in JavaScript. The constructor function allows us to easily define and standardize the instances we create. Good work!

Resources

fewpjs-adding-behavior-with-methods-nyc-web-career-031119's People

Contributors

drakeltheryuujin avatar maxwellbenton avatar sgharms avatar

Watchers

James Cloos avatar Kevin McAlear avatar  avatar Victoria Thevenot avatar Belinda Black avatar Bernard Mordan avatar  avatar Joe Cardarelli avatar Sara Tibbetts avatar The Learn Team avatar Sophie DeBenedetto avatar  avatar Antoin avatar Alex Griffith avatar  avatar Amanda D'Avria avatar  avatar Scott Ungchusri avatar Nicole Kroese  avatar Lore Dirick avatar Lisa Jiang avatar Vicki Aubin avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.