# Class.isClass

**URL:** <https://es.discourse.group/t/class-isclass/761>\
**Category:** 💡 Ideas\
**Tags:** proposal\
**Created:** [May 4, 2021, 12:18am UTC](https://es.discourse.group/t/class-isclass/761 "2021-05-04T00:18:54Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Matias](https://avatars.discourse-cdn.com/v4/letter/m/7ea924/32.png) [@Matias](https://es.discourse.group/u/Matias)\
**Post date:** [May 4, 2021, 12:18am UTC](https://es.discourse.group/t/class-isclass/761/1 "2021-05-04T00:18:55Z")

</div>

Hi Friends, what you think about to add to new standard the Class.isClass  
actually exists the Array.isArray....

---

<div class="post-metadata">

**Author:** ![ljharb](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/ljharb/32/8_2.png) [@ljharb](https://es.discourse.group/u/ljharb)\
**Post date:** [May 4, 2021, 2:23am UTC](https://es.discourse.group/t/class-isclass/761/2 "2021-05-04T02:23:37Z")

</div>

What is `Class`?

If you’re meaning to suggest every type have an “isWhatever” function on it, that’s been rejected in the past.

---

<div class="post-metadata">

**Author:** ![claudepache](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/claudepache/32/236_2.png) [@claudepache](https://es.discourse.group/u/claudepache)\
**Post date:** [May 4, 2021, 9:03am UTC](https://es.discourse.group/t/class-isclass/761/3 "2021-05-04T09:03:59Z")

</div>

If you intend to see if a particular object has been defined with the `class` keyword, you could look at its implementation, namely `C.toString().startsWith('class')`. But don’t do that: an object may have the functionality of a class without being defined with the `class` keyword.

SpiderMonkey once had a `Function.prototype.isGenerator()` method. It gave you a correct but unintuitive answer when `.bind()` was used around a generator function.

---

<div class="post-metadata">

**Author:** ![senocular](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/senocular/32/642_2.png) [@senocular](https://es.discourse.group/u/senocular)\
**Post date:** [May 4, 2021, 1:19pm UTC](https://es.discourse.group/t/class-isclass/761/4 "2021-05-04T13:19:42Z")

</div>

> [@claudepache](#):
>
> C.toString().startsWith('class')

Another reason not to do that because it could result in false positives

```javascript
(classes => classes).toString() // "classes => classes"

```

---

<div class="post-metadata">

**Author:** ![ljharb](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/ljharb/32/8_2.png) [@ljharb](https://es.discourse.group/u/ljharb)\
**Post date:** [May 4, 2021, 2:23pm UTC](https://es.discourse.group/t/class-isclass/761/5 "2021-05-04T14:23:08Z")

</div>

`typeof C === 'function' && Function.prototype.toString.call(C).startsWith('class')`

---

<div class="post-metadata">

**Author:** ![Matias](https://avatars.discourse-cdn.com/v4/letter/m/7ea924/32.png) [@Matias](https://es.discourse.group/u/Matias)\
**Post date:** [May 4, 2021, 2:47pm UTC](https://es.discourse.group/t/class-isclass/761/6 "2021-05-04T14:47:52Z")

</div>

oh, but, actually, the Array.isArray exists, why?

---

<div class="post-metadata">

**Author:** ![ljharb](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/ljharb/32/8_2.png) [@ljharb](https://es.discourse.group/u/ljharb)\
**Post date:** [May 4, 2021, 4:47pm UTC](https://es.discourse.group/t/class-isclass/761/7 "2021-05-04T16:47:00Z")

</div>

That's the only one that exists - it's because `instanceof Array` fails for cross-realm arrays (like, from an iframe), and it was intended to be a replacement for it for that use case. Unfortunately, it was not extended into a general mechanism.

---

<div class="post-metadata">

**Author:** ![Matias](https://avatars.discourse-cdn.com/v4/letter/m/7ea924/32.png) [@Matias](https://es.discourse.group/u/Matias)\
**Post date:** [May 5, 2021, 2:18am UTC](https://es.discourse.group/t/class-isclass/761/8 "2021-05-05T02:18:10Z")

</div>

friend, but, if the Array.isArray exists, then, for mathematical coherence, in the new ES6 Classes is needed to incorporate the Class.isClass, to determine if an object contains a class or not.  
"to clarify and normalize the language, and of course gain performance"  
Why appeal to ugly routines to achieve it?  
i actually use:  
function isClass(val)  
{  
return typeof val === "function" && /^\s\*class\s+/.test(val.toString());  
}

---

<div class="post-metadata">

**Author:** ![ljharb](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/ljharb/32/8_2.png) [@ljharb](https://es.discourse.group/u/ljharb)\
**Post date:** [May 5, 2021, 2:53am UTC](https://es.discourse.group/t/class-isclass/761/9 "2021-05-05T02:53:06Z")

</div>

That kind of consistency argument has been tried, and the committee resoundingly rejected it.

---

<div class="post-metadata">

**Author:** ![Matias](https://avatars.discourse-cdn.com/v4/letter/m/7ea924/32.png) [@Matias](https://es.discourse.group/u/Matias)\
**Post date:** [May 5, 2021, 2:54am UTC](https://es.discourse.group/t/class-isclass/761/10 "2021-05-05T02:54:03Z")

</div>

i see

---

<div class="post-metadata">

**Author:** ![theScottyJam](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/thescottyjam/32/616_2.png) [@theScottyJam](https://es.discourse.group/u/theScottyJam)\
**Post date:** [May 5, 2021, 3:04am UTC](https://es.discourse.group/t/class-isclass/761/11 "2021-05-05T03:04:11Z")

</div>

Why are you trying to distinguish classes from other objects? This isn't really possible to do in javascript.

For example:

```javascript
function MyThing() {
  const instance = Object.create(MyThing.prototype)
  Object.assign(instance, { x: 2, y: 3 })
  return instance
}

// MyThing is a class
const instance = new MyThing()
console.log(instance)

// MyThing is also a factory function
const instance2 = MyThing()
console.log(instance2)

```

The MyClass() function above is both a class and a factory function. There's nothing that can be done, even by the javascript language, to know weather or not that function is intended to be a class or not.

Or - maybe you're not trying to check if something is a class per-say, but are just trying to check if whatever you received was created using the es6 class syntax? Even this is dangerous to do - transpilers will automatically change classes made with es6 syntax into normal functions, and this isClass() function might not ever return true.

---

<div class="post-metadata">

**Author:** ![Matias](https://avatars.discourse-cdn.com/v4/letter/m/7ea924/32.png) [@Matias](https://es.discourse.group/u/Matias)\
**Post date:** [May 5, 2021, 3:18pm UTC](https://es.discourse.group/t/class-isclass/761/12 "2021-05-05T15:18:16Z")

</div>

Hi Scotty, i mean about the classes of ES6 or later.

---

<div class="post-metadata">

**Author:** ![Matias](https://avatars.discourse-cdn.com/v4/letter/m/7ea924/32.png) [@Matias](https://es.discourse.group/u/Matias)\
**Post date:** [May 5, 2021, 3:22pm UTC](https://es.discourse.group/t/class-isclass/761/13 "2021-05-05T15:22:44Z")

</div>

> > > Why are you trying to distinguish classes from other objects?  
> > > Sometimes is need to distinguish to decide either to cast a new instances of this or not,  
> > > this is common task in some frameworks, like ex: to apply skins etc  
> > > Imagine you can store a es6 class (the real class) inside a object variable, and later, with this, then cast a new instance calling these.

if isClass(val) -\> new val

---

<div class="post-metadata">

**Author:** ![claudepache](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/claudepache/32/236_2.png) [@claudepache](https://es.discourse.group/u/claudepache)\
**Post date:** [May 5, 2021, 4:31pm UTC](https://es.discourse.group/t/class-isclass/761/14 "2021-05-05T16:31:47Z")

</div>

I think that the best thing to do is to test whether your value is a function object, with:

```
typeof val === "function"

```

The test will pass for all constructors (including “classes”). That means, of course, that the user is not supposed to provide a function object that is not a constructor.

---

<div class="post-metadata">

**Author:** ![theScottyJam](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/thescottyjam/32/616_2.png) [@theScottyJam](https://es.discourse.group/u/theScottyJam)\
**Post date:** [May 5, 2021, 5:15pm UTC](https://es.discourse.group/t/class-isclass/761/15 "2021-05-05T17:15:49Z")

</div>

Another option here is to use a protocol. It sounds like what you're really wanting to know is whether or not the provided value implements some "casting" interface. (Not all classes would implement this behavior you're describing). So, if the provided value does implement it, then do one action, if they don't, then do another.

This duck-typing can be as simple as requiring anyone who implements this interface to have an explicit "cast" function on it. If it exists, then do your casting.

Or, to make it more robust, you can use a symbol:

```javascript
const cast = Symbol('Cast Protocol')

class Point {
  constructor(x, y) {
    this.x = x
    this.y = y
  }

  static [cast](somethingToCastFrom) {
    const { x, y } = someCastingLogic(somethingToCastFrom)
    return new Point(x, y)
  }
}

function castIfAble(value, CastClass = null) {
  // Does it implement the cast protocol?
  if (CastClass[cast]) {
    // Then use it
    return CastClass[cast](value)
  } else {
    // Otherwise, don't do any casting
    return value
  }
}

```

---

<div class="post-metadata">

**Author:** ![Matias](https://avatars.discourse-cdn.com/v4/letter/m/7ea924/32.png) [@Matias](https://es.discourse.group/u/Matias)\
**Post date:** [May 5, 2021, 5:29pm UTC](https://es.discourse.group/t/class-isclass/761/16 "2021-05-05T17:29:21Z")

</div>

scoty, I really appreciate your comment, but personally I think it is a very dirty solution ;)

---

<div class="post-metadata">

**Author:** ![ljharb](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/ljharb/32/8_2.png) [@ljharb](https://es.discourse.group/u/ljharb)\
**Post date:** [May 5, 2021, 5:35pm UTC](https://es.discourse.group/t/class-isclass/761/17 "2021-05-05T17:35:17Z")

</div>

You can `new` normal functions, and you can't `new` every class (for example, `class C { constructor() { if (new.target === C) { throw new Error('must be a subclass'); } } }`).

---

<div class="post-metadata">

**Author:** ![Matias](https://avatars.discourse-cdn.com/v4/letter/m/7ea924/32.png) [@Matias](https://es.discourse.group/u/Matias)\
**Post date:** [May 5, 2021, 5:42pm UTC](https://es.discourse.group/t/class-isclass/761/18 "2021-05-05T17:42:50Z")

</div>

Friend, you think in functions, but, i talk about es6 classes.  
The old way (funcion) is obsolete (I'm not looking to open a debate)  
Lets talk classes from es6 onwards

---

<div class="post-metadata">

**Author:** ![ljharb](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/ljharb/32/8_2.png) [@ljharb](https://es.discourse.group/u/ljharb)\
**Post date:** [May 5, 2021, 5:52pm UTC](https://es.discourse.group/t/class-isclass/761/19 "2021-05-05T17:52:28Z")

</div>

That’s how I’d describe “trying to generically determine if you can construct a value” ¯\_(ツ)\_/¯

---

<div class="post-metadata">

**Author:** ![ljharb](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/ljharb/32/8_2.png) [@ljharb](https://es.discourse.group/u/ljharb)\
**Post date:** [May 5, 2021, 5:54pm UTC](https://es.discourse.group/t/class-isclass/761/20 "2021-05-05T17:54:13Z")

</div>

It is not obsolete, it’s functionality that will exist forever and must work forever. A mechanism to determine constructability that ignores normal functions is broken and would not be acceptable in the language, full stop.

[Next page](https://es.discourse.group/t/class-isclass/761.md?page=2)
