npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

trait.js

v1.2.16

Published

Inherit methods from a trait instance to any model/class

Downloads

77

Readme

Trait.js is a helper which allows javascript to have the same behavior with the Php Trait feature. Allows you to inherit or reuse methods to any classes or objects. There's been other libraries/repositories out there that is also respresents the javascript equivalent of Php - Traits. Among them, this solution is a simple and lightweight package and serves only one purpose, allow any classes to inherit methods from multiple traits.

Installation

NPM
npm install trait.js --save

CDN

https://unpkg.com/[email protected]/build/trait.min.js

Sample Usage:

Let's create a simple trait that displays the user's fullname.

UserTrait.js

import trait from 'trait.js';

export default trait({
  getFullname() {
    return `${this.firstName} ${this.lastName}`;
  },
});

User.js

For example we have a user model that wants to use the UserTrait.js

import UserTrait from './UserTrait.js';

export default class User {
  constructor({ firstName, lastName }) {
    this.firstName = firstName;
    this.lastName = lastName;
  }
}

UserTrait.inheritTo(User);
// or
UserTrait.in(User); // a shorthand for inheritTo

Let's try to use that method:

import User from './User.js';

const user = new User({
  first_name: 'Foo',
  last_name: 'Bar',
});

// We can now use the trait method getFullname()
console.log(user.getFullname()); // Foo Bar

Why use Trait?

Javascript only supports single inheritance: a child class can inherit only from one single parent.

So, what if a class needs to inherit multiple behaviors? OOP traits solve this problem.

Traits are used to declare methods that can be used in multiple classes. Traits can have methods and abstract methods that can be used in multiple classes, and the methods can have any access modifier (public, private, or protected).

License

MIT © 2020 Regs Isabelo

FOSSA Status