mirror of
https://github.com/alrayyes/wiki.git
synced 2025-07-23 01:13:27 +00:00
Quartz sync: May 6, 2024, 10:40 PM
This commit is contained in:
parent
aee9145691
commit
4ef8371441
635 changed files with 22281 additions and 6 deletions
40
content/20201113094246-javascript_instanceof_operator.md
Normal file
40
content/20201113094246-javascript_instanceof_operator.md
Normal file
|
@ -0,0 +1,40 @@
|
|||
---
|
||||
id: 54c58f54-526f-4838-92c5-1a70d6b17a3c
|
||||
title: JavaScript Instanceof Operator
|
||||
---
|
||||
|
||||
# Description
|
||||
|
||||
Sometimes you want to know whether an [object](20200826201605-objects)
|
||||
was derived from a specific [class](20201008090316-class_notation). To
|
||||
do this one can use the `instanceof`
|
||||
[operator](20200613170705-operators_in_javascript).
|
||||
|
||||
# Syntax
|
||||
|
||||
``` javascript
|
||||
class Parent {
|
||||
constructor(name, parentChild = "Parent") {
|
||||
this.parentChild = parentChild;
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
speak(line) {
|
||||
console.log(`${this.parentChild} ${this.name} says '${line}'`);
|
||||
}
|
||||
}
|
||||
|
||||
class Child extends Parent {
|
||||
constructor(name) {
|
||||
super(name, "Child");
|
||||
}
|
||||
}
|
||||
|
||||
let parent = new Parent("Father");
|
||||
let child = new Child("Gregory");
|
||||
|
||||
console.log(parent instanceof Parent); // true
|
||||
console.log(parent instanceof Child); // false
|
||||
console.log(child instanceof Parent); // true
|
||||
console.log(child instanceof Child); // true
|
||||
```
|
Loading…
Add table
Add a link
Reference in a new issue