Code Monkey home page Code Monkey logo

node-obj-mix's Introduction

obj-mix

NPM version Build Status Coverage Status devDependencies

objMix is a simple utility that lets you merge methods, simple properties, and property descriptors into an object.

I've embraced JavaScript's prototypal nature and hate high-level class systems that try to turn JavaScript into something it is not. However I do like to simplify setting methods and properties onto a prototype. Up to now I've simply been using lodash's _.merge to do this. However I've recently been using getters in some node based projects. And while I can define these with Object.defineProperties it separates the definition of related properties and methods from each other.

objMix is a solution to that problem, call it with a base object such as a prototype and another object. objMix will merge functions and simple properties from the other object into that base object and use Object.defineProperty for anything that looks like a property descriptor.

var objMix = require('obj-mix');

function Foo() {
	// This is Foo
}

objMix(Foo.prototype, {
	bar: function() {
		return "This is a method.";
	},
	baz: "This is a property.",
	qux: {
		get: function() {
			return "This is a property with a getter.";
		}
	}
});

var foo = new Foo();
console.log(foo.bar()); // "This is a method.";
console.log(foo.baz); // "This is a property.";
console.log(foo.qux); // "This is a property with a getter.";

node-obj-mix's People

Contributors

dantman 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.