GithubHelp home page GithubHelp logo

hanss / composition-examples Goto Github PK

View Code? Open in Web Editor NEW

This project forked from learn-javascript-courses/composition-examples

0.0 2.0 0.0 306 KB

Examples for The Two Pillars of JavaScript: Prototypal OO

License: MIT License

JavaScript 100.00%

composition-examples's Introduction

composition-examples

Examples for The Two Pillars of JavaScript: Prototypal OO - Object Composition

Related Links

Courses & Webcasts

For in-depth video discussion of these examples with Q&A, watch the Composition with Prototypes Webcast Recording.

References

Chat

Learn About Prototypal OO

Libraries and Code

Prep Your Computer to Follow Along

What You'll Need:

  • Node v4+ (For the best experience on Mac and Linux, install with nvm)
  • Git
  • Your favorite text editor (or try Atom.IO)
  • This repo (see "Getting Started")

Getting Started

Clone & install:

git clone [email protected]:learn-javascript-courses/composition-examples.git
cd composition-examples
npm install

Start Dev Console

npm run watch

Experiment with the files in examples/ and test/. Whenever you save, the dev console will run lint and the test suite automatically.

You can exit the console by pressing CTRL+C (Win keyboard) or Command+C (Mac keyboard).

Testing Individual Examples

For your convenience, there are npm scripts set up in package.json:

    "----- Examples ----":"",
    "shape": "babel-node test/shape/index.js",
    "highpass": "babel-node test/highpass/index.js",
    "gorilla-banana/v1": "babel-node test/gorilla-banana/v1/index.js",
    "gorilla-banana/v1/fail": "babel-node test/gorilla-banana/v1/fail.js"
    ...

You can run these by typing:

npm run -s <examplename>

e.g.:

npm run -s gorilla-banana/v1

The -s option supresses the npm error page, which tends to scroll test failures off the top of the screen.

Some of the individual examples demonstrate test failures. They're excluded from the main test suite because they're expected to fail.

Practical Uses of Object Composition

Lots of people use animals as examples when they talk about inheritance techniques. I think some examples of real use-cases would be better. I commonly use object composition for a number of real-life things in every day app development, including:

Network & database I/O:

Connection objects hold state including host, port, accessToken, etc... A connection factory would typically return a connection object with an event emitter composed in to signal to the app when I/O takes place.

Configuration:

Typical production apps often get configuration data (such as details of network services it uses, etc...) from a variety of different sources. The app then shares configuration objects with the parts of the app that require it. I frequently compose in an event emitter to the configuration object as well so that we can log when parts of the app request configuration data that is undefined. This alerts us when parts of the app misbehave. It might look something like this:

const configuration = compose(source1, source2, source3, eventEmitter);

Assignment

Implement a scrum checkin app.

composition-examples's People

Contributors

ericelliott avatar

Watchers

 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.