Git Product home page Git Product logo

fewpjs-oo-static-methods-lab-online-web-pt-061019's Introduction

Static Methods Lab

Learning Goals

  • Write JavaScript static methods

Introduction

In this lab, we are going to practice writing static methods. In JavaScript, static methods aren't called on on instances of a class, but are called directly on a class itself.

Declaring a Static Method

To declare a static method, we write the word static inside the class declaration directly before a method's name:

class StaticMethod {
	static methodName() {
		console.log('My method is static!');
	}
}

Because static methods are called directly on the classes themselves, static methods have no access to data stored in specific objects.

Calling a Static Method

As the static method is operating on the class, you call the static method directly on the class.

ClassName.methodName();
// Calls the method explicitly on the class name itself and returns the `static` value

For static methods, this references the class. This means that you can call a static method from within another static method of the same class using this.

Instructions

You are going to write three static methods in the Formatter class. Write your code in the index.js file. Let the tests guide you through the process.

  1. Write a method static capitalize that takes in a string and capitalizes the first letter.
  2. Write a method static sanitize that takes in a string and removes all non-alphanumeric characters except for dashes, single quotes and spaces.
  3. Write a method static titleize that takes in a string and capitalizes all words in a sentence except the, a, an, but, of, and, for, at, by, and from; and always capitalizes the first word.

Conclusion

Static methods are useful ways to create utility methods for your data. If you have operation that you need do perform on a batch of data (say, capitalize all of the headlines on your website, as above), static methods are your go-to tool. Since they are called on the class but don't have access to individual objects, they are somewhat limited in their scope, but can be very powerful in the correct application.

Resources

fewpjs-oo-static-methods-lab-online-web-pt-061019's People

Contributors

cjbrock avatar maxwellbenton avatar thuyanduong-flatiron avatar dependabot[bot] avatar

Watchers

James Cloos avatar  avatar Mohawk Greene avatar Victoria Thevenot avatar Bernard Mordan avatar Otha avatar raza jafri avatar  avatar Joe Cardarelli avatar The Learn Team avatar Sophie DeBenedetto avatar  avatar  avatar Matt avatar Antoin avatar  avatar Alex Griffith avatar  avatar Amanda D'Avria avatar  avatar Ahmed avatar Nicole Kroese  avatar Kaeland Chatman 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.