我正在运行Edge/15.15063.'我可以使用'说const应该工作.
运行:
const x = 'woo'
Run Code Online (Sandbox Code Playgroud)
然后:
console.log(x)
Run Code Online (Sandbox Code Playgroud)
返回
'x' is undefined
Run Code Online (Sandbox Code Playgroud)
截图:
为什么const不工作?
我正在测试是否可以使用块作用域来替换 IIFE 以通过闭包创建“私有”变量。它一直运行良好,直到在 Safari 11.0.3 (11604.5.6.1.1) 中进行测试,该版本支持块作用域,但存在块和闭包的错误,例如:
{
let i = 0;
function getNext() {
return i++;
}
}
// Chrome and Firefox
console.log(getNext()); // 0
console.log(getNext()); // 1
// Safari
console.log(getNext()); // ReferenceError: Can't find variable: iRun Code Online (Sandbox Code Playgroud)
除了继续使用 IIFE 之外,还有其他解决方法吗?或者 Safari 是否正确而其他人都有错误?
附注。在严格模式下,getNext也没有在块外定义。
{function foo(){};foo=1;function foo(){};foo=2;}
console.log(foo); // 1Run Code Online (Sandbox Code Playgroud)
谁能解释为什么这里输出“1”?
编辑:似乎存在实现差异,在“Chrome”、“Firefox”、“Nodejs”中输出为“1”,但在“Safari”中输出为“2”
我正在阅读有关函数声明与函数表达式的内容,我无法弄清楚以下语句的含义:
函数声明作为独立构造出现,不能嵌套在非函数块中.
有人请用一个例子解释作者的意思,准确地说:"......不能嵌套在非功能块中".
链接是:https://javascriptweblog.wordpress.com/2010/07/06/function-declarations-vs-function-expressions/
Wrt提升fxn定义.
if (true) {
function foo() {
alert(1)
}
} else {
function foo() {
alert(2)
}
}
foo()Run Code Online (Sandbox Code Playgroud)
Chrome,大约2-3个月前 - 将打印2.现在,它打印1.我错过了什么,或者控制台停止在fxn上升!
DEMO - 打印1.我不知道在哪里可以找到旧浏览器版本的演示.可能是老款v8引擎的节点安装?目前的镀铬版 - 49
对于以下代码,既不是 ES6,也不是“严格模式”,我预期结果为 'b',因为 的第二个声明foo应该覆盖第一个声明。但结果是“a”!
{
function foo() {
console.log('a');
}
}
function foo() {
console.log('b');
}
foo(); // 'a' ? Why not 'b'?
Run Code Online (Sandbox Code Playgroud)
当此代码被附加花括号括起来时,结果是预期的“b”。
{ // additional curly braces
{
function foo() {
console.log('a');
}
}
function foo() {
console.log('b');
}
foo(); // 'b' as expected!
} // end additional curly braces
Run Code Online (Sandbox Code Playgroud)
为了进一步说明,请考虑以下附加示例:
foo('before declaration'); // outcome: from outside block :before declaration
{
function foo(s) {
console.log('from inside block: ' + s);
}
}
function foo(s) {
console.log('from …Run Code Online (Sandbox Code Playgroud) var a;
{
function a() {}
a = 60;
console.log('1: ', a);
}
console.log('2: ', a);
var b;
{
b = 60;
function b() {}
console.log('3: ', b);
}
console.log('4: ', b);Run Code Online (Sandbox Code Playgroud)
输出为:
1: 60
2: f a() {}
3: 60
4: 60
Run Code Online (Sandbox Code Playgroud)
我不知道为什么,如果除去花括号,它全部打印出来60。也许是因为吊装。但是据我所知,函数声明没有词法范围,即使有,它的第一个输出也应该打印函数,对吗?
我需要对屏幕后面发生的事情进行一些确认。
MDN 中有一篇文章说我们不应该在块级声明函数,例如在 if 语句中。因为它在整个浏览器中都是不一致的,并且与 ES2015 之前(或 ES6 之前)有关。
除非条件为真,否则不会创建 if 语句中的函数。
我想知道,如果条件为真,假设在5分钟后的JavaScript加载和同步设置后,将其创建的功能?为了创建函数,它是否仍然有代码的内存,或者它是否被转储到未使用的代码中?
我想知道即使在if语句完成后该函数是否仍然存在。可以访问吗?多久可以访问?在 if 条件为假之前是否可以访问?结果与 ES6 和 ES6 之前的结果不同吗?我听说 if 语句中没有 ES6 之前的范围。
例如
if (condition) {
function foo() {console.log(“hello world”);
}
}
在阅读 MDN 中关于“非严格代码中的块级函数”下的“函数”的文章后,我感到很困惑:https : //developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions
var a;
console.log('1 a: ' + a);
if (true) {
a = 1;
function a() { };
a = 5;
console.log('2 a: ' + a);
}
console.log('3 a: ' + a);Run Code Online (Sandbox Code Playgroud)
输出是:
1 a: undefined
2 a: 5
3 a: 1
Run Code Online (Sandbox Code Playgroud)
我无法理解最后的输出“3 a: 1”。为什么不是“3 a:5”?
虽然我在google中找不到对此的引用,但我很熟悉这样一个事实,即在javascript中,全局函数声明在任何代码执行之前都会得到解释.换句话说,这很好用:
f();
function f() {}
Run Code Online (Sandbox Code Playgroud)
但是,我注意到chrome和firefox对全局函数声明的解释有不同的解释.特别是,chrome很高兴在第一遍中读取一个if块内的函数声明,但firefox却没有.
try {document.write(f);} // works in chrome
catch(e) {document.write(e.message);} // throws an error in firefox
try {document.write(g);} // works in chrome and firefox
catch(e) {document.write(e.message);}
if(true) function f() {}
function g() {}
Run Code Online (Sandbox Code Playgroud)
你可以用这个小提琴自己尝试这个例子.我使用的是Chrome 16.0.912.75和Firefox 9.0.1.
这种行为的ECMA标准是什么?这个"提升"函数声明的过程是否有一个术语高于其他代码?什么代码被"解除"对解释是开放的(两种浏览器都是正确的)?或者是其中一个中的错误?
谁能告诉我它是如何逐步工作的,以及为什么结果不同:
第一的:
var a = 'foo';
function a() { };
console.log(a); // fooRun Code Online (Sandbox Code Playgroud)
第二:
// Uncaught SyntaxError: Identifier 'a' has already been declared
{
var a = 'foo';
function a() { };
}
console.log(a);Run Code Online (Sandbox Code Playgroud)
// **only no use strict!!!**
var a = 'foo';
console.log(a); // foo
{
console.log(a); // function a() {} - i guess that is the result of hoisting inside the block but on this step the global variable a …Run Code Online (Sandbox Code Playgroud)我知道这可能听起来有点荒谬,但我正在寻找一种方法来定义函数中的每个变量作为属性this.我正在寻找任何方法,以任何方式能够有一些方法来跟踪函数内的变量(即将它们添加到this对象),而不必实际为每个变量定义添加前缀this..有办法吗?这有可能Proxy吗?
function () {
// declare a variable
var hello = 'hi'
return this
}
let {hello} = function()
console.log(hello) // hi
Run Code Online (Sandbox Code Playgroud)
例如,这有效:
function hi () { this.hello = true; return this }
hi.bind({})() // { hello: true }
Run Code Online (Sandbox Code Playgroud)
我想要的是一种方法,在定义所有变量时hi将其添加到this对象中.
javascript ×13
ecmascript-6 ×2
hoisting ×2
scope ×2
closures ×1
ecmascript-5 ×1
firefox ×1
if-statement ×1
let ×1
safari ×1
this ×1