jbobject

JBObject is a library that provides functions related to JavaScript basic object in the browser.


Keywords
JBObject, jb, object, javascript, library, browser
License
MIT
Install
npm install jbobject@0.1.1

Documentation

JBObject

JBObject is a library that provides functions related to JavaScript basic object in the browser.

bind()

Creates and returns a new function that to be executed in the specified scope object.
A method that performs a feature similar to the Function.prototype.bind() method.

var foo = function() {
    return (this.name || null);
};
var bar = {name: 'SampleObject'};
var fnc = JBObject.bind(foo, bar);

console.log(foo()); // null
console.log(fnc()); // "SampleObject"
var foo = function(arg) {
    if (this.prop) {
        this.prop = arg;
        return this.prop;
    }
    return null;
};
var bar = {prop: 'SomeProperty'};
var fnc = JBObject.bind(foo, bar);

console.log(foo('OtherProperty')); // null
console.log(fnc('OtherProperty')); // "OtherProperty"

copy()

Copies and returns the target object. The copied object is different from the original object.

Changing the properties of the copied object does not change the properties of the original object.

If a shallow copy(if do not specify the second argument or if specify false), the objects inside the object are referenced rather than copied. If change the inner object of an object with shallowly copied, the inner object of the original object is also changed.

If a deep copy(the second argument is set to true), it will copy even the object inside the object. Even if change the inner object of an object with deep copied, the inner object of the original object does not change.

var foo = {name: 'SomeObject'};
var bar = JBObject.copy(foo);

console.log(bar); // {name: "SomeObject"}
console.log(foo === bar); // false
console.log(foo.name === bar.name); // true

A shallow copy example.

var foo = {
    name: 'SomeObject',
    deep: {
        deepProp: 'SomeDeepProperty'
    },
    prop: 'SomeProperty'
};
var bar = JBObject.copy(foo);
bar.name = 'OtherObject';
bar.deep.deepProp = 'OtherDeepProperty';

console.log(foo === bar); // false

console.log(foo.name === bar.name); // false
console.log(foo.name); // "SomeObject"
console.log(bar.name); // "OtherObject"

console.log(foo.deep.deepProp === bar.deep.deepProp); // true
console.log(foo.deep.deepProp); // "OtherDeepProperty"
console.log(bar.deep.deepProp); // "OtherDeepProperty"

A deep copy example.

var foo = {
    name: 'SomeObject',
    deep: {
        deepProp: 'SomeDeepProperty'
    },
    prop: 'SomeProperty'
};
var bar = JBObject.copy(foo, true);
bar.name = 'OtherObject';
bar.deep.deepProp = 'OtherDeepProperty';

console.log(foo === bar); // false

console.log(foo.name === bar.name); // false
console.log(foo.name); // "SomeObject"
console.log(bar.name); // "OtherObject"

console.log(foo.deep.deepProp === bar.deep.deepProp); // false
console.log(foo.deep.deepProp); // "SomeDeepProperty"
console.log(bar.deep.deepProp); // "OtherDeepProperty"

each()

Performs the specified function on each property of the target object and returns the result target object.

var foo = {
    a: 80,
    b: 70,
    c: 60,
    d: 50
};
var bar = function(prop, value) {
    this[prop] = value + 10;
};

console.log(foo); // {a: 80, b: 70, c: 60, d: 50}

var res = JBObject.each(foo, bar);
console.log(res); // {a: 90, b: 80, c: 70, d: 60}
var foo = {
    a: 80,
    b: 70,
    c: 60,
    d: 50
};
var bar = function(prop, value) {
    if (this.add) {
        foo[prop] = value + this.add;
    }
};
var baz = {add: 10};

console.log(foo); // {a: 80, b: 70, c: 60, d: 50}

var res = JBObject.each(foo, bar, baz);
console.log(res); // {a: 90, b: 80, c: 70, d: 60}

extend()

Extend the properties of the original object using the attributes of the target object.

Extend the attributes of the original object using the attributes of the target object to be used for expansion. A target object to be used for expansion can specify one or more objects.

var foo = {a: 1, b: 2};
var bar = {c: 3, d: 4};

JBObject.extend(foo, bar);
console.log(foo); // {a: 1, b: 2, c: 3, d: 4}
var foo = {a: 1, b: 2};
var bar = {b: 3, c: 4};

JBObject.extend(foo, bar);
console.log(foo); // {a: 1, b: 2, c: 4}
var foo = {a: 1, b: 2};
var bar = {b: 3, c: 4};

JBObject.extend(foo, bar, true);
console.log(foo); // {a: 1, b: 3, c: 4}
var foo = {a: 1, b: 2};
var bar = {b: 3, c: 4};
var baz = {a: 'a', z: 'z'};

JBObject.extend(foo, bar, baz);
console.log(foo); // {a: 1, b: 2, c: 4, z: "z"}
var foo = {a: 1, b: 2};
var bar = {b: 3, c: 4};
var baz = {a: 'a', z: 'z'};

JBObject.extend(foo, bar, baz, true);
console.log(foo); // {a: "a", b: 3, c: 4, z: "z"}
var foo = {a: 1, b: 2};
var bar = {b: 3, c: 4};
var baz = {a: 'a', z: 'z'};
var pop = {a: true};

JBObject.extend(foo, bar, baz, true, pop);
console.log(foo); // {a: "a", b: 2}

isArray()

Returns whether the specified object is of type Array.

console.log(JBObject.isArray([]));       // true
console.log(JBObject.isArray([1, 'A'])); // true

console.log(JBObject.isArray({}));       // false
console.log(JBObject.isArray(''));       // false
console.log(JBObject.isArray('ABC'));    // false

isObject()

Returns whether the specified object is of type Object.

console.log(JBObject.isObject({}));             // true
console.log(JBObject.isObject({a: 1, b: 'A'})); // true

console.log(JBObject.isObject([]));             // false
console.log(JBObject.isObject(null));           // false
console.log(JBObject.isObject(''));             // false
console.log(JBObject.isObject('ABC'));          // false
console.log(JBObject.isObject(0));              // false
console.log(JBObject.isObject(document));       // false

License

MIT License