Firebug将“删除”运算符应用于不合格的名称(Firebug gives applying the 'delete' operator to an unqualified name is deprecated)

我正在阅读有关使用JavaScript编写OO编程的书,并获得一些奇怪的行为:

function f1() { var a = 1; console.log('a in f1 function ', a); console.log('f2() called ', f2()); return 'f1 return value'; } function f2() { console.log('a value in f2() ', a); return a; } var a = 5; a = 55; var foo = 'bar'; console.log('delete a: ', delete a); console.log(a); console.log(f1()); console.log('delete window.f2: ', delete window.f2); console.log(f1()); console.log('delete foo: ', delete foo);

任何人都可以理解,为什么我的删除VARIABLE返回false(在Firefox中)和严格模式显示警告,如:

SyntaxError: applying the 'delete' operator to an unqualified name is deprecated console.log('delete foo: ', delete foo);

I'm reading book about OO programming in JavaScript and get some strange behaviour:

function f1() { var a = 1; console.log('a in f1 function ', a); console.log('f2() called ', f2()); return 'f1 return value'; } function f2() { console.log('a value in f2() ', a); return a; } var a = 5; a = 55; var foo = 'bar'; console.log('delete a: ', delete a); console.log(a); console.log(f1()); console.log('delete window.f2: ', delete window.f2); console.log(f1()); console.log('delete foo: ', delete foo);

Could anyone understand, why my delete VARIABLE return false (in Firefox) and strict mode display warning like:

SyntaxError: applying the 'delete' operator to an unqualified name is deprecated console.log('delete foo: ', delete foo);

最满意答案

您无法在javascript中删除普通变量。 您可以删除对象上的属性,但不能删除变量。 因此,不允许您尝试做的事情。 如果要释放该变量的内容(假设没有对其指向的数据的其他引用),则可以将变量设置为null 。

关于unqualified的消息部分可能只是指的是指定一个属性必须不仅仅是一个像你一样的非限定名称。 它必须有一个对象引用。

正如DCoder在评论中提到的,这是理解删除操作符的一个很好的参考 。

You can't delete a normal variable in javascript. You can delete a property on an object, but not a variable. Thus, what you're trying to do isn't allowed. If you want to free the contents of that variable (assuming there are no other references to the data it points to), you can just set the variable to null.

The part of the message about unqualified probably just refers to the fact that specifying a property would have to be more than just an unqualified name like you have. It would have to have an object reference.

As DCoder mentions in a comment, this is a good reference for understanding the delete operator.

Firebug将“删除”运算符应用于不合格的名称(Firebug gives applying the 'delete' operator to an unqualified name is deprecated)

我正在阅读有关使用JavaScript编写OO编程的书,并获得一些奇怪的行为:

function f1() { var a = 1; console.log('a in f1 function ', a); console.log('f2() called ', f2()); return 'f1 return value'; } function f2() { console.log('a value in f2() ', a); return a; } var a = 5; a = 55; var foo = 'bar'; console.log('delete a: ', delete a); console.log(a); console.log(f1()); console.log('delete window.f2: ', delete window.f2); console.log(f1()); console.log('delete foo: ', delete foo);

任何人都可以理解,为什么我的删除VARIABLE返回false(在Firefox中)和严格模式显示警告,如:

SyntaxError: applying the 'delete' operator to an unqualified name is deprecated console.log('delete foo: ', delete foo);

I'm reading book about OO programming in JavaScript and get some strange behaviour:

function f1() { var a = 1; console.log('a in f1 function ', a); console.log('f2() called ', f2()); return 'f1 return value'; } function f2() { console.log('a value in f2() ', a); return a; } var a = 5; a = 55; var foo = 'bar'; console.log('delete a: ', delete a); console.log(a); console.log(f1()); console.log('delete window.f2: ', delete window.f2); console.log(f1()); console.log('delete foo: ', delete foo);

Could anyone understand, why my delete VARIABLE return false (in Firefox) and strict mode display warning like:

SyntaxError: applying the 'delete' operator to an unqualified name is deprecated console.log('delete foo: ', delete foo);

最满意答案

您无法在javascript中删除普通变量。 您可以删除对象上的属性,但不能删除变量。 因此,不允许您尝试做的事情。 如果要释放该变量的内容(假设没有对其指向的数据的其他引用),则可以将变量设置为null 。

关于unqualified的消息部分可能只是指的是指定一个属性必须不仅仅是一个像你一样的非限定名称。 它必须有一个对象引用。

正如DCoder在评论中提到的,这是理解删除操作符的一个很好的参考 。

You can't delete a normal variable in javascript. You can delete a property on an object, but not a variable. Thus, what you're trying to do isn't allowed. If you want to free the contents of that variable (assuming there are no other references to the data it points to), you can just set the variable to null.

The part of the message about unqualified probably just refers to the fact that specifying a property would have to be more than just an unqualified name like you have. It would have to have an object reference.

As DCoder mentions in a comment, this is a good reference for understanding the delete operator.