有了这个页面:
<!DOCTYPE html>
<html>
<head>
<script>
"use strict";
var foo = 2;
delete foo;
</script>
</head>
<body></body>
</html>
Run Code Online (Sandbox Code Playgroud)
Firebug控制台给出:
applying the 'delete' operator to an unqualified name is deprecated
>>> foo
ReferenceError: foo is not defined
foo
Run Code Online (Sandbox Code Playgroud)
但是这很成功:
>>> var bar = 2;
undefined
>>> delete bar;
true
Run Code Online (Sandbox Code Playgroud)
即使您注释掉delete foo;脚本没有中断,删除bar仍然是成功的,尽管它"是Global对象的属性,因为它是通过变量声明创建的,因此具有DontDelete属性 ":
>>> foo
2
>>> delete foo
false
>>> var bar = 2;
undefined
>>> delete bar
true
Run Code Online (Sandbox Code Playgroud)
是否可以启用"严格模式"; 在FireBug和Chrome的控制台?
我试图在chrome控制台中编写一些ES6代码,但我遇到了一些错误.如何在控制台中运行ES6脚本?
例如,给定输入
let type='grizzle';
Run Code Online (Sandbox Code Playgroud)
控制台SyntaxError用消息记录a
在严格模式之外尚不支持块范围的声明(let,const,function,class)
如下面的截图所示
我的印象是"this"关键字代表范围内的当前所有者.显然,这是错误的.让我来看看代码:
alert(this); // alerts as [object Window] -- Okay
function p1() {
alert(this);
}
var p2 = function() {
alert(this);
}
p1(); // alerts as undefined -- ???
p2(); // alerts as undefined -- ??
window.p1(); // alerts as [object Window] -- Okay
window.p2(); // alerts as [object Window] -- Okay
Run Code Online (Sandbox Code Playgroud)
上面的代码首先警告[对象窗口],正如我所料,但接下来的两次调用p1()和p2()警告"this"为"undefined".最后两次调用p1()和p2()将"this"警告为[object window].
是不是p1()和p2()存在于全局(即窗口)范围内?我认为调用window.p1()与调用p1()同义,就像调用alert()与window.alert()同义一样.
对于我的(C#)思维方式,p1()和p2()属于全局范围.这些函数是全局窗口对象的成员,因此当它们引用"this"时,它们应该引用[object Window].显然,我在这里非常错.
从MDN:https : //developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/this,它说:
但是,在严格模式下,此值将保持为进入执行上下文时设置的值,因此,在以下情况下,它将默认为未定义:
function f2() {
'use strict'; // see strict mode
return this;
}
f2() === undefined; // true
Run Code Online (Sandbox Code Playgroud)
这表明如果我(1)“使用严格”;(2)在另一个函数中定义f2,调用f2将为f2绑定外部函数的this。但!
没用...
function f2() {
'use strict'; // see strict mode
return this;
}
f2() === undefined; // true
Run Code Online (Sandbox Code Playgroud)
给出以下输出:
hello, let's see some weird stuff
THIS in global {}
outer AlarmClock { clockName: 'Horizons' }
inner undefined
withoutOut { Moose: 'genius' }
modifiedTheThis, should have Howard { Moose: 'genius', howard: 'notSoBad' }
withoutIn1 undefined
Strict should …Run Code Online (Sandbox Code Playgroud)