Тёмный

5 things you need to know about delete operator in JavaScript | Interview guide 

Code Pro
Подписаться 2,7 тыс.
Просмотров 703
50% 1

source code here github.com/jayanthbabu123/5-t...
find out my blog here levelup.gitconnected.com/5-fa...
In JavaScript, the delete operator is employed to delete a property of an object. After deleting the actual property, that property won’t be accessible and returns undefined.
The invocation of the delete operator returns true when it removes a property and false otherwise. it’s only effective on an object’s properties, it has no effect on variable or function names.
The delete operator shouldn’t be used on predefined JavaScript object properties like window, Math, and Date objects. It can crash your application.
Let’s scrutinize some facts about the delete operator.
Delete object properties
The only way to fully remove the properties of an object in JavaScript is by using delete operator.
If the property which you’re trying to delete doesn’t exist, delete won’t have any effect and can return true.
Can we delete variables in Javascript?
The delete operator removes a property from an object. It cannot delete a variable. Any property declared with var cannot be deleted from the global scope or from a function's scope.
If you declare a variable without var, it can be deleted. Let’s look into the example below.
The variable declared without the var keyword internally stores it as a property of the window object. So we can delete the properties of the window object.
Can we delete values from an array?
Since JavaScript arrays are objects, elements can be deleted by using delete.
delete will delete the object property, but will not reindex the array or update its length. This makes it appear as if it is undefined.
Using delete may leave undefined holes in the array. Use pop(), shift(), or splice() instead.
Deleting built-in objects like Math, Date, and window objects is unsafe, and it can crash your entire application.
Deleting non-configurable properties
Object properties, besides a value, has three special attributes:
writable - if true, the value can be changed, otherwise it’s read-only.
enumerable - if true, it is listed in loops, otherwise not listed.
configurable - if true, the property can be deleted or the attributes can be modified, otherwise it cannot be changed.
The values assigned by using Object.defineProperty and set to configurable: false in an object cannot be deleted.
In strict mode, it will throw an error if you try to delete a non-configurable property.
delete is the only true way to remove an object’s properties without any leftovers, but it works ~100 times slower if you are using delete in loops.
The alternative solution is setting the value to undefined like object[key] = undefined. It doesn’t fully delete the property, it just sets value to undefined. the choice isn’t exactly a prominent solution, but if you utilize it with care then you’ll be able to improve the performance.

Опубликовано:

 

20 июн 2020

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии    
Далее
PORTAL SPAMMER🤬🤬🤬| Doge Gaming
00:19
Просмотров 1 млн
🤡Украли У ВСЕХ🤪
00:37
Просмотров 251 тыс.
25 Nooby Pandas Coding Mistakes You Should NEVER make.
11:30
Learn SQL In 60 Minutes
56:24
Просмотров 2,1 млн
ALL React Hooks Explained in 12 Minutes
12:21
Просмотров 105 тыс.
Github - You Can View Deleted Private Fork Data
23:40
Pydantic Tutorial • Solving Python's Biggest Problem
11:07
Why The Windows Phone Failed
24:08
Просмотров 232 тыс.