IonIon
Packages
  • next
  • current
Changelog
GitHub
Packages
  • next
  • current
Changelog
GitHub
  • Version 0.x

    • Release Notes
    • Upgrade Guide
    • Contribution Guide
    • Security Policy
    • Code of Conduct
    • Origin
  • Packages

    • Introduction
    • Container

      • Introduction
      • Prerequisites
      • How to install
      • Container Instance
      • Bindings
      • Dependencies
      • Resolving
      • Contextual Bindings
    • Contracts

      • Introduction
      • How to install
    • Support

      • Introduction
      • How to install
      • Arrays

        • About Arrays
        • Includes All
        • Includes Any
        • Is Array Like
        • Is Concat Spreadable
        • Is Safe Array Like
        • Is Typed Array
        • Merge
      • Concerns

        • About Concerns
        • Prerequisites
        • Concern Class
        • Using Concerns
        • Aliases
        • Conflict Resolution
        • Booting
        • Hooks
        • Edge Cases
        • JSDoc
      • Exceptions

        • About Exceptions
        • Configure Custom Error
        • Configure Stack Trace
        • Get Error Message
        • Custom Errors
      • Facades

        • About Facades
      • Meta

        • About Meta
        • Prerequisites
        • Supported Elements
        • Set & Get
        • Inheritance
        • Outside Changes
        • TC39 Proposal
        • Target Meta
      • Mixins

        • About Mixins
        • New Mixin
        • Apply Mixins
        • Instanceof
        • Inheritance
        • Onward
      • Object

        • About Objects
        • Forget
        • Forget All
        • Get
        • Has
        • Has All
        • Has Any
        • Has Unique ID
        • Is Cloneable
        • Is Populatable
        • Isset
        • Merge
        • Populate
        • Set
        • Unique ID
      • Reflections

        • About reflections
        • Assert Has Prototype Prop.
        • Class Looks Like
        • Class Own Keys
        • Get All Parents Of Class
        • Get Class Prop. Descriptor
        • Get Class Prop. Descriptors
        • Get Constructor Name
        • Get Name Or Desc. Tag
        • Get Parent Of Class
        • Has All Methods
        • Has Method
        • Has Prototype Property
        • Is Callable
        • Is Class Constructor
        • Is Class Method Reference
        • Is Constructor
        • Is Key Safe
        • Is Key Unsafe
        • Is Method
        • Is Subclass
        • Is Subclass Or Looks Like
        • Is WeakKind
      • Misc

        • About Misc.
        • Desc. Tag
        • Empty
        • Is Key
        • Is Primitive
        • Is Property Key
        • Isset
        • Merge Keys
        • To Weak Ref.
      • Callback Wrapper
    • Vuepress Utils

      • Introduction
      • How to install
      • Navigation

        • Archive
      • Plugins

        • Last Updated
      • Components

        • Version Disclaimer
    • XYZ (test package)

classLooksLike Available since v0.9

Determines if a target class "looks like" the provided class "blueprint".

  • Arguments
  • Class Blueprint
  • Recursive

Arguments

classLooksLike() accepts the following arguments:

  • target: object - the target class object.
  • blueprint: ClassBlueprint - a blueprint that defines the expected members of a class (see Class Blueprint for details.).
import { classLooksLike } from '@aedart/support/reflections';

class A {}

class B {
    foo() {}
}

const blueprint = { members: [ 'foo' ] };

classLooksLike(A, blueprint); // false
classLooksLike(B, blueprint); // true

Class Blueprint

The class "blueprint" is an object that defines the expected members (property keys) of a target class. All defined members must exist in target class' prototype, before the classLooksLike() returns true.

You can specify either or both of the following properties in a class blueprint object:

  • members: PropertyKey[] - (optional) Properties or methods expected to exist in class' prototype.
  • staticMembers: PropertyKey[] - (optional) Properties or methods expected to exist in class as static members.

Note: If you do not specify either members or staticMembers, then a TypeError is thrown.

class A {
    foo() {}

    bar() {}
}

class B {
    foo() {}
    
    static bar() {}
}

const blueprint = { members: [ 'foo' ], staticMembers: [ 'bar' ] };

classLooksLike(A, blueprint); // false
classLooksLike(B, blueprint); // true

Recursive

classLooksLike() traverses target class' prototype chain. This means that you can compare a subclass against a blueprint and inherited members will automatically be included in the check.

class A {
    foo() {}
}

class B extends A {
    bar() {}
}

const blueprint = { members: [ 'foo', 'bar' ]};

classLooksLike(A, blueprint); // false
classLooksLike(B, blueprint); // true
Edit page
Last Updated:
Contributors: alin
Prev
Assert Has Prototype Prop.
Next
Class Own Keys