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

remember-this

v0.2.0

Published

Easily create functions bound to a specific object

Downloads

8

Readme

remember-this

Easily create functions bound to a specific object.

Purpose

JavaScript changes the value of the this variable depending upon how a function is invoked. For example:

var person = {
  name: "Brian",
  getName: function() {
    return this.name;
  }
}

// Prints "Brian"
console.log(person.getName());


var getNameDetached = person.getName;

// Prints "undefined"
console.log(getNameDetached());

Why did it print "undefined"? Because getNameDetached was not invoked through the person object. Instead, it was invoked as a stand-alone function, so its this variable was bound to JavaScript's global object. To fix this, you would instead need to do:

// Manually bind the function back to person
var getNameAttached = person.getName.bind(person);

// Prints "Brian"
console.log(getNameAttached());

In other words: you need to get the function from person, then remember to bind it back to person. This is easy to forget, and it can introduce some very bizarre bugs into your code.

But, good news! remember-this provides a couple of functions that encapsulate this pattern so that your life can be awesome!

Installation

npm install remember-this

Usage

Let's pretend that I'm wiring together my routes for an Express.js application. I want the routes to call the methods (erm... functions) on my usersController object. I can achieve this as follows:

var router           = express.Router();
var usersControllers = new UsersController(...);


// rt stands for "Remember This"
var rt = require('remember-this').rt;

// Creates the function c(funcName).  It grabs
// usersController[funcName] and binds it back to
// usersController.
var c = rt(usersController);


// Equivalent to:
//   router.get('/', usersController.index.bind(usersController));
//
router.get('/', c('index'));


// Equivalent to:
//   router.get('/:id', usersController.show.bind(usersController));
//
router.get('/:id', c('show'));

In other words: I used the rt(...) function to create the c(...) function. The c(...) function grabs methods (erm... functions) from usersController and binds them back to usersController.

If you want to keep the number of local variables down to a minimum, then you can pass a callback to the rt(...) function instead. For example:

var router           = express.Router();
var usersControllers = new UsersController(...);


// rt stands for "Remember This"
var rt = require('remember-this').rt;


rt(usersController, function(c) {
  router.get('/', c('index'));
  router.get('/:id', c('show'));
});

Or, maybe your usersController only contains 1 method that you want to pass to your router. In that case, you can use the bound function instead:

var router           = express.Router();
var usersControllers = new UsersController(...);

var bound = require('remember-this').bound;

router.get('/:id', bound(usersController, "show"));