How to check if a variable is an object without its own properties defined by the developer?

There is a variable in my code that is sometimes one of the following:

  • number,
  • boolean
  • line,
  • regular expression,
  • date of,
  • An object with 0 or more "native" properties.

I want to check if this variable is an "elementary object". I define an "elementary object" as:

  • number,
  • boolean
  • line,
  • regular expression,
  • date of,

or, in other words, an object that does not have its own properties defined by the developer.

Object.getOwnPropertyNames(obj).length === 0, ( " " ) "" . , Object.getOwnPropertyNames("test") Chrome (46.0.2490.86 (64- ) Ubuntu 15.10) DevTools Firefox 42 Ubuntu 15.10, ["0", "1", "2", "3", "length"] ( Chrome) [ "length", "0", "1", "2", "3" ] ( Firefox).

, , " " , ? , .

! :-)

Update:

, , , - , - .

function isElementaryValue(x) {
    return typeof x === "number" ||
        typeof x === "boolean" ||
        typeof x === "string" ||
        x instanceof RegExp ||
        x instanceof Date;
}
+4
1

, :

function isElementary (arg) {
  return arg === null
    || arg === undefined 
    || (typeof arg !== 'object' && typeof arg !== 'function') 
    || arg instanceof RegExp 
    || arg instanceof Date;
}
+2

Source: https://habr.com/ru/post/1617621/


All Articles