Expand README for plugin-transform-instanceof [skip ci] (#7827)
This commit is contained in:
parent
b28ffedead
commit
1de36948ac
@ -1,5 +1,29 @@
|
|||||||
# @babel/plugin-transform-instanceof
|
# @babel/plugin-transform-instanceof
|
||||||
|
|
||||||
|
> Wraps `instanceof` expressions to allow constructors to customize the logic with a `Symbol.hasInstance` method
|
||||||
|
|
||||||
|
## Example
|
||||||
|
|
||||||
|
**In**
|
||||||
|
|
||||||
|
```javascript
|
||||||
|
foo instanceof Bar;
|
||||||
|
```
|
||||||
|
|
||||||
|
**Out**
|
||||||
|
|
||||||
|
```javascript
|
||||||
|
function _instanceof(left, right) {
|
||||||
|
if (right != null && typeof Symbol !== "undefined" && right[Symbol.hasInstance]) {
|
||||||
|
return right[Symbol.hasInstance](left);
|
||||||
|
} else {
|
||||||
|
return left instanceof right;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
_instanceof(foo, Bar);
|
||||||
|
```
|
||||||
|
|
||||||
## Installation
|
## Installation
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
@ -31,3 +55,8 @@ require("@babel/core").transform("code", {
|
|||||||
plugins: ["@babel/plugin-transform-instanceof"]
|
plugins: ["@babel/plugin-transform-instanceof"]
|
||||||
});
|
});
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## References
|
||||||
|
|
||||||
|
* [ES6 Spec: InstanceOf Operator Semantics](https://www.ecma-international.org/ecma-262/6.0/#sec-instanceofoperator)
|
||||||
|
* [MDN: Symbol.hasInstance](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol/hasInstance)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user