Skip to content
Snippets Groups Projects
Commit 87dceb28 authored by Deucе's avatar Deucе :ok_hand_tone4:
Browse files

Aha! null is an object that isn't an Object!

var x = null;
if (typeof x == 'object')
	print(Object.keys(x).join(', '));
parent bd61ccb2
No related branches found
No related tags found
1 merge request!463MRC mods by Codefenix (2024-10-20)
......@@ -51,7 +51,7 @@ function TickITCfg() {
function lcprops(obj)
{
if(typeof obj == 'object' && !Array.isArray(obj)) {
if(typeof obj == 'object' && obj !== null) {
var i;
var keys = Object.keys(obj);
......
  • Developer

    if (obj) {

    should be enough of a check here... since, apparently it's Object.keys(obj) that's being used of interest and that should work on anything that isn't null/undefined.

  • Developer

    Or as a short if (!obj) return null though haven't looked at the rest of this function in particular, I just tend to prefer to exit early as a pattern.

  • Author Maintainer

    Many truthy things can be non-objects, so testing for thruthiness isn't useful. The test is there specifically to find things that can be passed to Object.keys() without an exception.

    The else case is an implied return... expanding to something like:

    if (typeof obj !== 'object')
        return
    if (obj === null)
        return

    would be needlessly verbose.

    I generally prefer a single return as a pattern except in cases where indentation gets too deep or when goto would be required. Hunting for returns when debugging is a PITA.

  • Developer

    @Deuce

    Anything that is truthy (and some that aren't, like false) can be passed to Object.keys() .. so that's the only check that is needed.

    > Object.keys(true)
    []
    > Object.keys(1)
    []
    > Object.keys(new Date())
    []
    > Object.keys(false)
    []
    > Object.keys([])
    []

    For that matter, you could just omit the check, and use Object.keys(Object(obj)) which will work for anything, including null.

0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment