任何人都可以为我分解如何(这看起来很简单)是由浏览器解释的:
var a = 1;
function b() {
a = 10;
function a() {}
}
b();
alert(a);
Run Code Online (Sandbox Code Playgroud)
它将带来1.如果我将一个函数名称改为其他任何东西,等等:
var a = 1;
function b() {
a = 10;
function m() {}
}
b();
alert(a);
Run Code Online (Sandbox Code Playgroud)
它会提醒10.
比方说,我们有一个Dog类:
class Dog {
static food;
private static static_var = 123;
constructor(private name) {}
speak() {
console.log(this.name + ', I eat ' + Dog.food + ', ' + Dog.static_var);
}
}
Run Code Online (Sandbox Code Playgroud)
编译成JS:
var Dog = (function () {
function Dog(name) {
this.name = name;
}
Dog.prototype.speak = function () {
console.log(this.name + ', I eat ' + Dog.food + ', ' + Dog.static_var);
};
Dog.static_var = 123;
return Dog;
})();
Run Code Online (Sandbox Code Playgroud)
这同样有效并且不那么复杂:
function Dog(name) {
this.name = name;
} …Run Code Online (Sandbox Code Playgroud) 我有一个如下的Javascript代码,http://jsfiddle.net/ramchiranjeevi/63uML/
var foo = 1;
function bar() {
foo = 10;
return;
function foo() {}
}
bar();
console.log(foo); // returns 1
Run Code Online (Sandbox Code Playgroud)
执行代码时,将调用bar()函数,并使用值10覆盖全局变量,然后将日志打印为10,而不是将其打印为值1。
我想知道 javascript 提升如何用于全局变量。
假设我有以下代码片段:
var a = 5;
function print(){
console.warn("a",a,b);
var a = 10;
b=5;
console.warn("a",a);
}
print();
Run Code Online (Sandbox Code Playgroud)
在这种情况下,我收到错误“b 未定义”。我想知道为什么 Javascript 提升不适用于全局变量。我试图寻找这个,但只得到变量提升的结果。有什么想法吗??
我根本不是一个javascript noob,虽然在我的一生中我从未遇到过这个,但我是否正确地假设javascript必须在运行任何东西之前分配函数?
根据我的经验,我希望这会返回'undefined',但很明显它会返回'function'.
function bar() {
return foo;
foo = 10;
function foo() {}
var foo = '11';
}
alert(typeof bar());
Run Code Online (Sandbox Code Playgroud)
有人能为我解释一下吗?
嗨,我想了解JavaScript的基本原理,并陷入一个条件.
var foo = 1;
function bar(){
foo = 10;
return;
function foo(){}
}
bar();
alert(foo);
Run Code Online (Sandbox Code Playgroud)
在这里alert(foo),会给我1,并且我知道在return语句之后,函数foo()将不会执行.但是现在如果改变代码:
var foo = 1;
function bar(){
foo = 10;
return;
}
bar();
alert(foo);
Run Code Online (Sandbox Code Playgroud)
在条形函数中,如果我将删除函数foo().然后alert(foo)会给我10
请帮忙,如果有人能解释我为什么?
我需要了解浏览器中的JavaScript引擎如何读取函数.请考虑以下代码:
var g = 5;
alert(g); //alerts 5
function haha() {
alert(g); // alerts NOTHING. Why is this? Why not alert 5? var g = 45 hasn't yet occurred
var g = 45;
alert(g); // alerts 45
};
haha();
alert(g); // alerts 5. Because g = 45 is a local variable.
Run Code Online (Sandbox Code Playgroud)
这是一个非常令人困惑的行为我发现我无法弄清楚:
var foo = 'outside';
function logIt(){
console.log(foo);
var foo = 'inside';
}
logIt();
Run Code Online (Sandbox Code Playgroud)
这将产生未定义.这对我来说已经无法解释了.但陌生人是这样的:
var foo = 'outside';
function logIt(){
console.log(foo);
}
logIt();
Run Code Online (Sandbox Code Playgroud)
实际上会在外面屈服.
为什么会这样?
在一次采访中,我被要求猜测以下代码片段的输出:
var foo = 1;
function bar() {
foo = 10;
return;
function foo() {
}
}
bar();
console.log(foo);Run Code Online (Sandbox Code Playgroud)
我认为输出将是 10,因为 bar 函数在重新分配 foo 的值后立即返回,但实际输出为 1。是否因为函数定义具有相同的变量名(foo)?是否为foo里面的变量创建了一个新的作用域bar?
javascript ×10
scope ×3
var ×2
closures ×1
function ×1
hoisting ×1
jquery ×1
typescript ×1
variables ×1