0.1.0 • Published 4 years ago

pseudoprimitive v0.1.0

Weekly downloads
-
License
MIT
Repository
github
Last release
4 years ago

PseudoPrimitive

PseudoPrimitive is a small library for creating objects act like primitives. This can be used to create primitives which have properties attached to them.

Installation

npm i pseudoprimitive

Usage

import { PseudoPrimitive } from "pseudoprimitive"

const myObject = {}

// Define a PseudoPrimitive in field myObject.foo
PseudoPrimitive(myObject, "foo", 0)

myObject.foo.bar = function() {
  return this + 1
}
myObject.foo // Number(0)
myObject.foo++
myObject.foo // Number(1)
myObject.foo.bar() // 2
myObject.foo = 123
myObject.foo // Number(123)
myObject.foo.bar() // 124

Caveats

  • A PseudoPrimitive is not a true primitive, and therefore typeof returns "object". Its behaviour is effectively that of the builtin wrapper types Number, String, Boolean etc.
  • Since PseudoPrimitive relies on getters and setters, it can only be used as an object property. To create a PseudoPrimitive variable, you can create properties on the global object. However, this is not recommended.
0.1.0

4 years ago