Finished Chapter 6.4 Deleting Properties

master
Jason Zhu 2021-01-12 13:04:05 +11:00
parent 8e664f922d
commit 0ab5798d1b
1 changed files with 52 additions and 0 deletions

View File

@ -232,7 +232,59 @@ Lists of tips:
## 6.4 Deleting Properties
`delte` operator removes a property from an obj. Its operand is *property access expression*
`delete` & prototypes:
* only delete own properties, cannot delete inherited ones (from prototypes).
* deleting inherited properties need to it on prototype, which has affect all children obj.
`delete` return true if:
* delete succeeded
* when delete had no effect (e.g.
* on inherited properties
* non-exist properties
* when used with an expression that's not a property access expression
```js
let o = {x: 1}; // o has own property x and inherits property toString
delete o.x // => true: deletes property x
delete o.x // => true: does nothing (x doesn't exist) but true anyway
delete o.toString // => true: does nothing (toString isn't o's own property)
delete 1 // => true: nonsense, but true anyway
```
`delete` fails if:
* trying to rm properties that have a `configurable` attribute of `false`.
* trying to delete non-configurable properties of built-in objects
* properties of the global object created by variable declaration and function declaration
`delete`'s failure in strict & non-strict mode:
* In strict mode: causes TypeError
* In non-strict mode: evaluate to `false` e.g. shown below
```js
// In strict mode, all these deletions throw TypeError instead of returning false
delete Object.prototype // => false: property is non-configurable
var x = 1; // Declare a global variable
delete globalThis.x // => false: can't delete this property
function f() {} // Declare a global function
delete globalThis.f // => false: can't delete this property either
```
`delete` configurable properties of global object:
* In non-strict mode, via directly delete property name
```js
globalThis.x = 1; // Create a configurable global property (no let or var)
delete x // => true: this property can be deleted
```
* In strict mode, method above will create **SyntaxError**. Hence need to be explicit:
```js
delete x; // SyntaxError in strict mode
delete globalThis.x; // This works
```
## 6.5 Testing Properties