2013-05-20 15 views
36

私はかなりjavascriptを初めて使いましたが、私はそれが危険で高速かつゆるやかな表現力に魅力的です。つまり、「厳密な使用」モードで操作すると、オブジェクトを削除できないことがわかりました。私は物事を削除するという巨大なファンではありません(理論的には、スコープはそれをどうにかして処理する必要があるからです)が、この機能を削除する背景には何があったのでしょうか?Javascript5厳密モードで削除が許可されないのはなぜですか?

+3

例を詳細に説明している:[MDN厳密モード(https://developer.mozilla.org/en- US/docs/JavaScript/Reference/Functions_and_function_scope/Strict_mode) "*エラーをエラーに変換する*" –

答えて

59

deleteステートメントは厳密モードでは許可されていますが、特定の用途には間違いがあります。これは、単純な名前ではなくオブジェクトのプロパティに対してのみ許可され、削除可能なオブジェクトのプロパティに対してのみ許可されます。

したがって

var a = {x: 0}; 
delete a.x; 

は正常であるが、

delete Object.prototype; 

を試みている間(後者は、実際の構文レベルのエラーではないではない、どちらも

delete a; 

あります削除不能なプロパティを削除するのは実行時エラーです)

+4

興味深い。なぜ実際のvar自体を削除できないのでしょうか? – sircodesalot

+2

@sircodesalotローカル変数を削除するのはかなり変だからです。これは、コードブロックのセマンティクスを記述することをはるかに困難にします。 (私は実際に "正式な"論理的根拠を見たことはありません) – Pointy

+8

@sircodesalot:スコープ内で変数のバインディングを変更することはできません。スコープに 'var foo'が含まれている場合、そのスコープ内の' foo'へのすべての参照は、そのスコープ用に作成された変数を参照します。さもなければ、外部スコープ内の変数 'foo'を参照します(存在する場合)。変数は実行文によって作成されるものではなく、それらによっても破棄されることはありません。 – supercat

0

[削除]でまたよく説明だ

// The delete statement is still allowed in strict mode, but some particular uses of it are erroneous. It's only allowed for object properties, not simple names, and only for object properties that can be deleted. 
 

 
// "use strict"; 
 

 
// creates the property adminName on the global scope 
 
adminName = "xyz"; 
 

 
// creates the property empCount on the global scope 
 
// Since we are using var, this is marked as non-configurable. The same is true of let and const. 
 
var empCount = 43; 
 

 
EmployeeDetails = { 
 
    name: "xyz", 
 
    age: 5, 
 
    designation: "Developer" 
 
}; 
 

 
// adminName is a property of the global scope. 
 
// It can be deleted since it is created without var. 
 
// Therefore, it is configurable. 
 
console.log("delete adminName =", delete adminName); // returns true 
 

 
// On the contrary, empCount is not configurable, 
 
// since var was used. 
 
console.log("delete empCount =", delete empCount); // returns false 
 

 
// delete can be used to remove properties from objects 
 
console.log("delete EmployeeDetails.name =", delete EmployeeDetails.name); // returns true 
 

 
// Even when the property does not exists, it returns "true" 
 
console.log("delete EmployeeDetails.salary =", delete EmployeeDetails.salary); // returns true 
 

 
// delete does not affect built-in static properties 
 
console.log("delete Math.PI =", delete Math.PI); // returns false 
 

 
// EmployeeDetails is a property of the global scope. 
 
// Since it defined without "var", it is marked configurable 
 
console.log("delete EmployeeDetails =", delete EmployeeDetails); // returns true 
 

 
x = 1; 
 
var y = 2; 
 

 
function f() { 
 
    var z = 44; 
 

 
    console.log("delete x =", delete x); // returns true 
 
    console.log("delete y =", delete y); // returns false 
 
    // delete doesn't affect local variable names 
 
    console.log("delete z =", delete z); // returns false 
 
} 
 

 
f.call();

関連する問題