Skip to main content
AI Assist is now on Stack Overflow. Start a chat to get instant answers from across the network. Sign up to save and share your chats.
deleted 11 characters in body
Source Link
run_the_race
  • 2.6k
  • 3
  • 59
  • 92

Oh My God! I think this could be moreeven shorter than ever, let see this:

Short and Final code

function isObject(obj)
{
    return obj != null && obj.constructor.name === "Object"
}

console.log(isObject({})) // returns true
console.log(isObject([])) // returns false
console.log(isObject(null)) // returns false

Explained

Return Types

typeof JavaScript objects (including null) returns "object"

console.log(typeof null, typeof [], typeof {})

Checking on Their constructors

Checking on their constructor property returns function with their names.

console.log(({}).constructor) // returns a function with name "Object"
console.log(([]).constructor) // returns a function with name "Array"
console.log((null).constructor) //throws an error because null does not actually have a property

Introducing Function.name

Function.name returns a readonly name of a function or "anonymous" for closures.

console.log(({}).constructor.name) // returns "Object"
console.log(([]).constructor.name) // returns "Array"
console.log((null).constructor.name) //throws an error because null does not actually have a property

Note: As of 2018, Function.name might not work in IE

Oh My God! I think this could be more shorter than ever, let see this:

Short and Final code

function isObject(obj)
{
    return obj != null && obj.constructor.name === "Object"
}

console.log(isObject({})) // returns true
console.log(isObject([])) // returns false
console.log(isObject(null)) // returns false

Explained

Return Types

typeof JavaScript objects (including null) returns "object"

console.log(typeof null, typeof [], typeof {})

Checking on Their constructors

Checking on their constructor property returns function with their names.

console.log(({}).constructor) // returns a function with name "Object"
console.log(([]).constructor) // returns a function with name "Array"
console.log((null).constructor) //throws an error because null does not actually have a property

Introducing Function.name

Function.name returns a readonly name of a function or "anonymous" for closures.

console.log(({}).constructor.name) // returns "Object"
console.log(([]).constructor.name) // returns "Array"
console.log((null).constructor.name) //throws an error because null does not actually have a property

Note: As of 2018, Function.name might not work in IE

I think this could be even shorter:

Short and Final code

function isObject(obj)
{
    return obj != null && obj.constructor.name === "Object"
}

console.log(isObject({})) // returns true
console.log(isObject([])) // returns false
console.log(isObject(null)) // returns false

Explained

Return Types

typeof JavaScript objects (including null) returns "object"

console.log(typeof null, typeof [], typeof {})

Checking on Their constructors

Checking on their constructor property returns function with their names.

console.log(({}).constructor) // returns a function with name "Object"
console.log(([]).constructor) // returns a function with name "Array"
console.log((null).constructor) //throws an error because null does not actually have a property

Introducing Function.name

Function.name returns a readonly name of a function or "anonymous" for closures.

console.log(({}).constructor.name) // returns "Object"
console.log(([]).constructor.name) // returns "Array"
console.log((null).constructor.name) //throws an error because null does not actually have a property

Note: As of 2018, Function.name might not work in IE

Oh My God! I think this could be more shorter than ever, let see this:

Short and Final code

function isObject(obj)
{
    return obj != null && obj.constructor.name === "Object"
}

console.log(isObject({})) // returns true
console.log(isObject([])) // returns false
console.log(isObject(null)) // returns false

Explained

Return Types

typeof JavaScript objects (including null) returns "object"

console.log(typeof null, typeof [], typeof {})

Checking on Their constructors

Checking on their constructor property returns function with their names.

console.log(({}).constructor) // returns a function with name "Object"
console.log(([]).constructor) // returns a function with name "Array"
console.log((null).constructor) //throws an error because null does not actually have a property

Introducing Function.name

Function.name returns a readonly name of a function or "anonymous" for closures.

console.log(({}).constructor.name) // returns "Object"
console.log(([]).constructor.name) // returns "Array"
console.log((null).constructor.name) //throws an error because null does not actually have a property

Note: As of 2018, Function.name might not work in IE https://developer.mozillaAs of 2018, Function.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/name#Browser_compatibilityname might not work in IE

Oh My God! I think this could be more shorter than ever, let see this:

Short and Final code

function isObject(obj)
{
    return obj != null && obj.constructor.name === "Object"
}

console.log(isObject({})) // returns true
console.log(isObject([])) // returns false
console.log(isObject(null)) // returns false

Explained

Return Types

typeof JavaScript objects (including null) returns "object"

console.log(typeof null, typeof [], typeof {})

Checking on Their constructors

Checking on their constructor property returns function with their names.

console.log(({}).constructor) // returns a function with name "Object"
console.log(([]).constructor) // returns a function with name "Array"
console.log((null).constructor) //throws an error because null does not actually have a property

Introducing Function.name

Function.name returns a readonly name of a function or "anonymous" for closures.

console.log(({}).constructor.name) // returns "Object"
console.log(([]).constructor.name) // returns "Array"
console.log((null).constructor.name) //throws an error because null does not actually have a property

Note: As of 2018, Function.name might not work in IE https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/name#Browser_compatibility

Oh My God! I think this could be more shorter than ever, let see this:

Short and Final code

function isObject(obj)
{
    return obj != null && obj.constructor.name === "Object"
}

console.log(isObject({})) // returns true
console.log(isObject([])) // returns false
console.log(isObject(null)) // returns false

Explained

Return Types

typeof JavaScript objects (including null) returns "object"

console.log(typeof null, typeof [], typeof {})

Checking on Their constructors

Checking on their constructor property returns function with their names.

console.log(({}).constructor) // returns a function with name "Object"
console.log(([]).constructor) // returns a function with name "Array"
console.log((null).constructor) //throws an error because null does not actually have a property

Introducing Function.name

Function.name returns a readonly name of a function or "anonymous" for closures.

console.log(({}).constructor.name) // returns "Object"
console.log(([]).constructor.name) // returns "Array"
console.log((null).constructor.name) //throws an error because null does not actually have a property

Note: As of 2018, Function.name might not work in IE

[Edit removed during grace period]
Source Link
Erisan Olasheni
  • 2.9k
  • 1
  • 23
  • 21
deleted 17 characters in body
Source Link
Erisan Olasheni
  • 2.9k
  • 1
  • 23
  • 21
Loading
deleted 1 character in body
Source Link
Erisan Olasheni
  • 2.9k
  • 1
  • 23
  • 21
Loading
Make example more shorter.
Source Link
Erisan Olasheni
  • 2.9k
  • 1
  • 23
  • 21
Loading
Makes example more shorter.
Source Link
Erisan Olasheni
  • 2.9k
  • 1
  • 23
  • 21
Loading
corrected spellings
Source Link
Erisan Olasheni
  • 2.9k
  • 1
  • 23
  • 21
Loading
deleted 13 characters in body
Source Link
Erisan Olasheni
  • 2.9k
  • 1
  • 23
  • 21
Loading
Added a reference link
Source Link
Erisan Olasheni
  • 2.9k
  • 1
  • 23
  • 21
Loading
Added a reference link
Source Link
Erisan Olasheni
  • 2.9k
  • 1
  • 23
  • 21
Loading
Source Link
Erisan Olasheni
  • 2.9k
  • 1
  • 23
  • 21
Loading