Do not remove the attribute from JavaScript objects

Based on the EcmaScript specification, there are certain object properties that are supposed to be undeletable due to the internal parameter DontDelete. For example:

var y = 5

should not be able to be deleted. However, upon further investigation, it seems that it can be deleted.

Refer to the Mozilla Developer Center for more information: https://developer.mozilla.org/en/JavaScript/Reference/Operators/Special/delete

Any thoughts on why this functionality is not behaving as expected?

Answer №1

It's important to verify the information you come across. The ECMA-specification (262, ed 5) does not include an internal parameter called DontDelete. Perhaps they are referring to the [Configurable] property instead? Remember, the delete operator is used for object properties, not variables or functions:

var y=5, 
    z = {y:5};
delete y;
delete z.y;
alert(y);   //=> 5
alert(z.y); //=> undefined

My response to this topic led to the discussion in this Stack Overflow question, where T.J. Crowder provided an insightful answer.

Answer №2

As per the specifications in ES5 table 17:

CreateMutableBinding(N, D) This method creates a new mutable binding within an environment record. The value of N represents the name of the binding. If the optional Boolean argument D is set to true, then the binding may be deleted at a later time.

Furthermore, in section 10.5 Declaration Binding Instantiation

  1. For each VariableDeclaration and VariableDeclarationNoIn found in the code, proceed in source text order as follows: [...] ii. Invoke the env’s SetMutableBinding method with the parameters dn, undefined, and strict.

This information implies that declared variables are intended to be non-deletable. In the case of global code, the activation object is the global object, which acts as the variable object. Therefore, declared global variables should not be deletable. However, it is worth noting that some browsers may not strictly adhere to this rule.

Answer №3

let x = 10;
alert(delete(x));

Display true. Afterwards, cannot be deleted.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Asynchronous Return in NodeJS Class Methods

Currently, I am in the process of developing a JavaScript class that includes a login method. Here is an overview of my code: const EventEmitter = require('events'); const util = require('util'); const Settings = require('./config ...

AngularJS combined with MVC is causing an issue where the table fails to refresh following an HTTP post request

When working with an $http.post() call and trying to update an HTML table in the success() callback, I encountered an issue: .success(function (data) { $scope.categories.push(data); }); Here is the HTML code for the table: <tbody> ...

Struggling to enable Google Cast functionality on Apache Cordova: Unhandled error - chrome is not recognized

Struggling to integrate Google Cast with Apache Cordova, I'm facing challenges due to outdated guides and plugins. Despite finding a recently updated plugin three months ago, I keep encountering this error: Uncaught ReferenceError: chrome is not defi ...

Transform the JavaScript function to a Node.js module

My function serves as an abstract factory for creating JavaScript objects. Here is the code: var $class = function(definition) { var constructor = definition.constructor; var parent = definition.Extends; if (parent) { var F = function ...

What is the process for transferring multiple files using a read stream in Node.js?

Suppose I have a folder containing two files, index.html and style.css, and I would like to send both of them. How can I achieve this with Node.js? Router.get('/', function(req, res) { var indexStream = fs.createWriteStream('path to index ...

What is the cost associated with using the require() function in an Express.js application?

I have a web application built with Express.js that serves one of my domains. The structure of the app.js file is as follows: var express = require('express'); var app = express(); // and so on… To incorporate one of my custom functions in t ...

What is the process for incorporating a collection in Mongoose?

Trying to clear the Users collection before and after tests: before(function(done) { mongoose.connection.collections['users'].drop(function(err) { mongoose.connection.collections['users'].insert(user, done); }); }); after(func ...

Tips for aligning a select and select box when the position of the select has been modified

Recently, I encountered an interesting issue with the default html select element. When you click on the select, its position changes, but the box below it fails to adjust its position accordingly. https://i.stack.imgur.com/SwL3Q.gif Below is a basic cod ...

Utilizing PHP to Monitor Devices Sharing the Same Network or IP Address

I am currently working on creating a conversion tracking page with postback in PHP. In order to do this, I need to generate a unique transaction ID for each unique click. One method I am using to track unique clicks is by capturing the user's IP addre ...

What is the best way to send an array and file in the same AJAX request?

When attempting to send both an image file and an array through my AJAX request to a PHP script, I encountered an issue where either the array or the image file doesn't appear. The problem seems to stem from the specific lines that need to be added to ...

Conceal a table with jQuery

Is there a way to initially conceal a table on page load only to reveal it with a sliding animation when a specific link is clicked? I have attempted using the following code to hide the table: $('.table_div').hide(); I have enclosed the table ...

Contrast between Q.defer() and the Promise() function

I am puzzled by the differing behavior of the following code when using Q.defer() as opposed to Promise() Scenario 1 : When Q.defer() is used getDocument(id) .then(function (response) { console.log('in first then') return 'from tw ...

Passing parameters to a new page in AngularJS using ng-click event

At the top of my page, I have three buttons with an input box underneath. <div> <form> <div> Enter Show Name<input type="text" ng-model="showName" /> </div> </form> </div> ...

How to access the api variable in JavaScript

When attempting to retrieve variables from an API object, I encountered the obstacle of them being nested inside another object named "0" in this particular case. Here is a screenshot from the console: enter image description here Below is the JavaScrip ...

Struggling with Getting My Animation to Function Correctly

I am trying to create a JQuery Animation that will move a div covering a text box up into the border when clicked. Despite multiple attempts, I can't seem to get the animation to work properly. Any suggestions? JavaScript function moveup(title,text ...

Disabling modal popup buttons in Bootstrap 4 using Javascript

JSFiddle DEMO: https://jsfiddle.net/2yx16k8L/ I'm encountering an issue with my JS code. I want a click on a div to open the entire content within it. However, this action is causing the modal button in the dropdown to stop working. How can I resolve ...

Method for transferring all items to a new array in AngularJs

There are 2 arrays: $scope.first = [ { fName:'Alex', lName='Doe' }, { fName:'John', lName='S' } ] var second= [ { fName:'Tom', lName='M', email:'<a href="/cdn-cgi/l/email-protectio ...

Tips for avoiding a 500 GET error due to an empty request during page loading

I recently encountered an issue with a form containing a dependent drop-down menu that reloads after form submission to preselect the main choice that was made before submission. The problem arises when the page initially loads without any parameters being ...

Deactivate user input in Knockout Kendo TimePicker

Is it possible to prevent user input in the Kendo UI TimePicker while using knockout-kendo binding? In a previous project without knockout-kendo, I was able to achieve this by using the following code (see jsfiddle example): $('#timepicker').at ...

Guide on accessing a nested array within a JSON object using JavaScript

I'm currently faced with a json object that contains nested arrays and names with spaces like Account ID. My goal is to only display the Account ID's within my Vue.js application. While I can access the entire response.data json object, I'm ...