Skip to content

Added new method for Object class, Object #isObject #289

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions src/prototype/lang/object.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
NUMBER_TYPE = 'Number',
STRING_TYPE = 'String',
OBJECT_TYPE = 'Object',
OBJECT_CLASS = '[object Object]';
FUNCTION_CLASS = '[object Function]',
BOOLEAN_CLASS = '[object Boolean]',
NUMBER_CLASS = '[object Number]',
Expand Down Expand Up @@ -486,6 +487,24 @@
return object instanceof Hash;
}

/**
* Object.isObject(object) -> Boolean
* - object (Object): The object to test.
*
* Returns `true` if `object` is of type [[Object]]; `false` otherwise.
*
* ##### Examples
*
* Object.isObject({});
* //-> true
*
* Object.isObject('str');
* //-> false
**/
function isObject(object) {
return _toString.call(object) === OBJECT_CLASS;
}

/**
* Object.isFunction(object) -> Boolean
* - object (Object): The object to test.
Expand Down Expand Up @@ -603,6 +622,7 @@
keys: Object.keys || keys,
values: values,
clone: clone,
isObject: isObject,
isElement: isElement,
isArray: isArray,
isHash: isHash,
Expand Down
13 changes: 13 additions & 0 deletions test/unit/tests/object.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,19 @@ suite('Object', function () {
assert.strictEqual(false, Object.isElement(undefined));
});

test('.isObject', function () {
assert(Object.isObject({}));
assert(Object.isObject(new Object()));

assert(!Object.isObject("a string"));
assert(!Object.isObject($(document.createElement('div'))));
assert(!Object.isObject([]));
assert(!Object.isObject(0));
assert(!Object.isObject(false));
assert(!Object.isObject(undefined));
assert(!Object.isObject(/xyz/));
});

test('.isFunction', function () {
assert(Object.isFunction(function() { }));
assert(Object.isFunction(Class.create()));
Expand Down