我正在读"你不懂JS"系列的第二本书,我读过在变量之前函数被提升了.
所以这是代码:
foo(); // 1
var foo;
function foo() {
console.log( 1 );
}
foo = function() {
console.log( 2 );
};Run Code Online (Sandbox Code Playgroud)
这个输出将是1.但为什么呢?首先提升函数,然后提升变量.因此,在我的函数foo(打印1的那个)被提升之后,它必须跟随变量foo.所以结果应该是"未定义"而不是"1".
我希望代码的行为就像它一样:
// hoisted first
function foo() {
console.log( 1 );
}
// hoisted second
var foo; // implicitly initialized to 'undefined'
foo(); // call 'undefined' - error?
foo = function() {
console.log( 2 );
};
Run Code Online (Sandbox Code Playgroud)
这里发生了什么事?
这是代码:
class Program
{
static void Main(string[] args)
{
double varrr = Divide(10, 0);
}
static double Divide(double a, double b)
{
double c = 0;
try
{
c = a / b;
return c;
}
catch (DivideByZeroException)
{
Console.WriteLine("Division by zero not allowed");
return 0;
}
}
}
Run Code Online (Sandbox Code Playgroud)
我原以为除以零会抛出 aDivideByZeroException但它没有,当我在控制台上打印结果时,输出是“Infinity”。这是为什么?
如果没有明确的演员,这段代码如何运作?
static void Main()
{
IList<Dog> dogs = new List<Dog>();
IEnumerable<Animal> animals = dogs;
}
Run Code Online (Sandbox Code Playgroud)
我在谈论这一行:
IEnumerable<animal> animals = dogs;
Run Code Online (Sandbox Code Playgroud)
你可以在这里看到我能够在没有明确演员的情况下传递变量狗.但这怎么可能呢?为什么编译器允许我这样做?我不应该先做这样的演员:
IEnumerable<Animal> animals = (List<Dog>) dogs;
Run Code Online (Sandbox Code Playgroud)
代码将使用显式强制转换而不使用显式强制转换但我无法理解为什么它允许我在没有显式强制转换的情况下将狗分配给animals引用变量.
这是代码:
<html>
<head>
<title>The CSS Box Model</title>
</head>
<body>
<section>
<h1>An h1 inside of a section</h1>
</section>
</body>
</html>Run Code Online (Sandbox Code Playgroud)
到目前为止,我所知道的是有一个盒子模型。每个元素都是由盒模型组成的。Box Model 有 4 个部分:内容、填充、边框和边距。所以在这部分代码中:
<section>
<h1>An h1 inside of a section</h1>
</section>
Run Code Online (Sandbox Code Playgroud)
该部分的内容实际上是表示它的 h1 的整个框 + 它的边距(h1 的)。是这样吗?