JavaScript中delete操作符不能刪除的對象

來源:互聯網
上載者:User

ES3 中,delete在8.6.2.5及11.4.1有介紹,如下

 

有一些資訊,

1、實現上delete操作符會調用引擎內部的[[Delete]]方法

2、[[Delete]]在8.6.2裡定義

3、刪除的屬性有個DontDelete的特性,如果有,delete時直接返回false

 

搜尋“DontDelete”,會發現有很多,如下都不能delete

 

1, 啟用物件的arguments對象 (10.1.6)

function func() {delete arguments;alert(arguments);}func(1);

 

2,變數聲明 (10.2.1)

var a = 10;delete a;alert(a); // 10

這一條在很多JS書裡有提及,即不能delete掉使用var聲明的變數。

 

3,函式宣告

function func() {}delete func;alert(func); // func code

 

4,函數的length屬性

function func(a, b) {}delete func.length;alert(func.length); // 2

 

5,一些常量(NaN、Infinity、undefined)

delete NaN; // falsedelete Infinity; // falsedelete undefined; // false

 

6,內建構造器的prototype

delete Object.prototype; // falsedelete Function.prototype; // falsedelete Array.prototype; // falsedelete ExpReg.prototype; // falsedelete Date.prototype; // falsedelete Error.prototype; // falsedelete Number.prototype; // falsedelete Boolean.prototype; // falsedelete String.prototype; // false

 

7, 數組和字串的length

var arr = [], str = 'hello';delete arr.length; // falsedelete str.length; // false

 

8,Math對象的屬性(Math.E、Math.LN10、Math.LN2、Math.LOG2E、Math.LOG10E、Math.PI、Math.SQRT1_2、Math.SQRT2)

delete Math.E; // false...

  

9,正則對象的屬性(source、global、ignoreCase、multiline、lastIndex)

var reg = /ss/;delete reg.source; // false...

  

ES5 與ES3不同,ES5中沒有“DontDelete”,卻增加了 [[Configurable]] (8.6.1)。

如果該值為false,則不能delete,以上列舉的9點在ES5中描述為[[Configurable]]為false。

 

ES5新增的Object.defineProperty方法可顯示的定義對象的Configurable,如下

var obj = {name: 'John'};Object.defineProperty(obj, "key", {  configurable: false,  value: "static"});delete obj.name; // truedelete obj.key // false

對象obj有name,key。name可以delete,key則不行。

 

此外ES5strict 模式中delete configuable為false的對象時會直接拋異常。如

"use strict";delete Object.prototype;

FF中控制台報錯如下

 

除了內建對象的一些方法或屬性不能刪除外,自訂對象也有不能刪除的。如delete不能刪除對象繼承來自原型上的屬性

function Person() {}Person.prototype.name = 'John Backus';var p = new Person();delete p.name;console.log(p.name); // 仍然輸出 John Backus

如果this和prototype上都有name,那麼delete後,會將prototype上的呈現出來

function Person() {this.name = 'John Backus';}Person.prototype.name = 'John Resig';var p = new Person();console.log(p.name); // John Backusdelete p.name;console.log(p.name); // John Resig, 來自原型

如果非要刪除原型上的name,只能

delete Person.prototype.name

 

總結下:

1,內建對象的屬性及方法多數不能delete(雖然有些能delete,如isNaN、parseInt)

2,對象繼承於原型的屬性和方法不能delete 

 

原因也很簡單,

1,內建對象的屬性及方法多數不能delete保護該語言最核心API,這些API被delete了,基本上就廢了。如delete Object.prototype。

2,對象繼承於原型的屬性和方法不能delete是出於保護原型,否則 “類A的對象delete了原型上的屬性,那麼繼承於A的都將丟失該屬性”。

 

相關:

JavaScript聲明全域變數三種方式的異同

JavaScript中兩種類型的全域對象/函數

https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Object/defineProperty

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.