Skip to content
A js object super extension.
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Type Name Latest commit message Commit time
Failed to load latest commit information.


A js object super extension.


  • get/set properties by using keyPath
  • responsive, watch changes by keyPath
  • data version, create/reset snapshots
  • data validation
  • data lock, immutable after locked


Install & Usage

Install the pacakge in your project:

npm i objext

And then import it in your code:

import Objext from 'objext'

Or use CommonJS:

const { Objext } = require('objext')

Or use in browser:

<script src="node_modules/objext/dist/objext.js"></script>
const { Objext } = window['objext']

And then create a objext instance:

const objx = new Objext({
  name: 'tomy',
  age: 10,
  body: {
    head: true,
    feet: true,
  get sex() {
    return this.age > 8 ? 'male' : 'female'
  say() {

As you seen, just pass a normal js object into the constructor to create a objext instance.

Now you can use objx as a normal object, however, you can use more feature.

Data operation with keyPath

In normal js object, you can read a property by using obj.prop, however when you read a deeper property like obj.body.feet, if obj.body is undefined, you will get an TypeError. Using objext $get to avoid this.


Get data by keyPath. keyPath is a string which is the path to get the deep property value.

It will return the value you want to get, notice, if the property is a computed property, the return value will be the computed result.

let feet = objx.$get('body.feet')
let feet = objx.body.feet

The difference between the previous sentences is, if original objx.body.feet is an object, objx.body.feet will return an objext instance, not the original object, so that you can use $get on it too.

$set(keyPath, value)

Set data by keyPath.

objx.$set('body.feet', 1)
objx.body.feet = 1

However, if objx.body is not defined, it will be set as an object automaticly:

console.log(objx.body) // undefined
objx.$set('body.feet', 1)
console.log(objx.body) // an objext instance which has 'feet' property

Notice, directly add a property to an objext instance is not allowed too, you must use $set or $update to add new properties.


Remove data by keyPath.


Notice, it is completely not like delete operation. delete objx.body.feet is not allowed when you using objext, because it will broken reactive feature of objext.

Responsive data

If you have used Vue or Mobx, you may be familar with responsive data. Objext support responsive data, and you can watch the change of data change.

$watch(keyPath, callback, deep)

It is very like angular's $watch method:

objx.$watch('', (e, newValue, oldValue) => {
  // ...

Then I use $set or directly set to change its value, the callback function will run:

objx.$set('', 'black') = 'black'

The paramater e of callback contains:

  • oldValue
  • newValue
  • path: the real changed property's keyPath
  • key: the value of $watch's first parameter keyPath
  • target: the parent node of changed property
  • preventDefault(): stop invoke other callbacks of real keyPath
  • stopPropagation(): stop invoke callbacks of this property's parents and ancestors
  • isEqual(): a helper function to compare oldValue and newValue

So you can do like this:

objx.$watch('', ({ oldValue, newValue, isEqual }) => {
  if (!isEqual(oldValue, newValue)) {
    // do something...

deep is to set watch children properties.

objx.$watch('body', ({ path }) => {
  console.log(path + ' changed') // => ' changed'
}, true) = 'red' // this will trigger the callback function too

When you want to watch any change, you can set keyPath to be *:

objx.$watch('*', () => {
  // run when any property changed

$unwatch(keyPath, callback)

Remove the watcher.

Computed properties

Objext supports computed properties, and you can watch the change of computed properties too:

const objx = new Objext({
  age: 10,
  get height() {
    return this.age * 12.5
objx.$watch('height', (e, newValue, oldValue) => {
objx.age = 20 // the $watch callback will run, because objx.height is dependent on objx.age

Notice, it only supports getter, setter will be dropped if you give.

Version controlable data

When you use objext to manage some data, your data may change always. However, sometimes you want to recover previous data. Objext provides a more modern version control of data like git do.


Create a snapshot of current data with name by tag.


Recover data with the snapshot whose name is tag. = 'tomy'
objx.$commit('version1') = 'jimy'
console.log( // => 'tomy'


Remove snapshots whose name is tag. If you want to remove all snapshots, don't pass tag.


Objext provide ability to validate data.


Set validate rules. validators is an array, which contains items has the following structure:

  path: 'body.head',
  check: value => value !== null, // should return boolean
  message: 'should not be null', // when validate fail, return this message to warn
  warn: error => {}, // the function to run when validate fail,


Run all validators, if one failed, will not run the left ones.

Data lock

You can lock data before some operation.


Lock data, then data is not writable with objext api. = 'tomy'
objx.$lock() = 'pina'
console.log( // => 'tomy'


Unlock data.

Other APIs

You can use methods as chain like:

objx.$silent(true).$set('name', 'lily').$silent(false)


Reset all data to be new. The current properties will be removed firstly, and new data will be set into objext instance.


Update existing data one time, watchers will only run after all data updated. If some properties do not exist, they will be added to the data.


Change the silent mode. When silent mode is on, no watchers will be trigger when data change.

objx.$watch('name', () => console.log('name changed.'))
objx.$set('name', 'lily') // this will not trigger watcher because of silent mode

$start() / $end()

During batch updating, watchers will not be triggered until $end() run.

// the following $set will not trigger watchers
objx.$set('body.main', 'left')
objx.$set('name', 'ceci')
objx.$set('body.main', 'right')
// when $end() run, all watchers will be invoked.
// the watchers of 'body.main' will only run once with final value 'right'

$update and $put use $start/$end inside.


Clone current objext instance to a new objext instance.


A identity of current objext instance, you can it to compare two objext instances:

if (objx1.$hash === objx2.$hash) {
  // ...


Get current objext instance's original js object.


Get current objext instance's original js object's json string.

You can’t perform that action at this time.