可能重复:
为什么var在"foreach(table.Rows中的var row)"中评估为System.Object?
今天发现以下内容我感到非常惊讶....
SqlDataReader reader = cmd.ExecuteReader();
DataTable schemaTable = reader.GetSchemaTable();
// the following compiles correctly
foreach (DataRow field in schemaTable.Rows)
{
Console.WriteLine(field["ColumnName"]);
}
// the following does not compile as 'var' is of type 'object'
foreach (var field in schemaTable.Rows)
{
// Error: Cannot apply indexing with [] to an expression of type 'object'
Console.WriteLine(field["ColumnName"]);
}
Run Code Online (Sandbox Code Playgroud)
这里发生了什么?
这是一种类型推断失败吗?如果是这样,是什么原因造成的?
或者它是定义的行为的一部分还是var?如果是这样,为什么?
我认为这个想法var是你可以在变量声明/初始化的任何地方使用它而不改变行为.
我得到这样的窗口高度:
var theHeight = "innerHeight" in window
? window.innerHeight
: document.documentElement.offsetHeight;
Run Code Online (Sandbox Code Playgroud)
我需要一个高度减去50px的var
我怎么做到的?
谢谢!
function f1() {
for (i = 0; i <= 5; i++)
console.log(i);
}
function foo() {
for (i = 0; i < 5; i++)
f1();
}
foo();
Run Code Online (Sandbox Code Playgroud)
嗨,我试图理解为什么执行foo的结果是:
0
1
2
3
4
5
Run Code Online (Sandbox Code Playgroud)
并不是:
0
1
2
3
4
5
0
1
2
3
4
5
0
1
2
3
4
5
0
1
2
3
4
5
0
1
2
3
4
5
Run Code Online (Sandbox Code Playgroud)
这是我正在阅读的关于JS的幻灯片,它讨论的是当你不使用var然后它在全局对象上定义并提供这个例子而没有任何进一步的细节我们得到结果.
我以为它会简单地循环并运行f1函数,直到它小于5.
请帮我理解.
谢谢
有这个问题的解决方案吗?
class ViewController : UIViewController {
let collectionFlowLayout = UICollectionViewFlowLayout()
let collectionView = UICollectionView(frame: CGRectZero, collectionViewLayout: collectionFlowLayout)
}
Run Code Online (Sandbox Code Playgroud)
xcode给我以下错误
ViewController.swift: 'ViewController.Type' does not have a member named 'collectionFlowLayout'
Run Code Online (Sandbox Code Playgroud)
我可以使它成为一个可选项并在init方法中初始化它,但我正在寻找一种方法来使collectionview成为let而不是var
我有这个代码:
$(document).ready(function() {
$('.fa-crosshairs').click(function() {
$('*').click(function() {
var currentclass = $(this).attr('class');
});
});
$('#1color').click(function() {
$('body').css({
"background-color": "black"
});
});
});
Run Code Online (Sandbox Code Playgroud)
我需要获取currentclass var,然后使用它代替$('body').css,但我不知道我该怎么做.
关键是通过单击获取一个元素,然后在我点击('#1color')时更改其CSS
假设我有一段这样的代码:
const number = 3;
function fooFunction() {
let numberTwo = 5;
var answer = number + numberTwo;
return answer;
}
finalAnswer = fooFunction();
console.log(finalAnswer);
Run Code Online (Sandbox Code Playgroud)
假设与ES2015兼容的浏览器,使用上述代码的优点/缺点是:
const number = 3;
function fooFunction() {
var numberTwo = 5;
var answer = number + numberTwo;
return answer;
}
finalAnswer = fooFunction();
console.log(finalAnswer);
Run Code Online (Sandbox Code Playgroud)
是否有任何优点或缺点,因为它们都返回相同的数字?
在查看此处显示的var功能之后:
我在使用JDK 10设置Eclipse/IntelliJ IDEA IDE时遇到了困难,因此请求具有可用Java 10环境的Stack Overflow用户提供帮助.
考虑以下:
public class A {
public void someMethod() { ... }
}
public class B extends A{
@Override
public void someMethod() { ... }
}
...
...
...
var myA = new A(); // Works as expected
myA = new B(); // Expected to fail in compilation due to var being
// syntactic sugar for declaring an A type
myA = (A) (new B()); // Should work
myA.someMethod(); // …Run Code Online (Sandbox Code Playgroud) 我已经安装了2018.1版本的IntelliJ IDEA的(社区版),这增加了对支持Java的10.
当我尝试使用新的"var"来键入局部变量时,IDE会用红色和可爱的短语突出显示它:"无法解析类型"var".请参阅附件 varRed
我已经阅读了另一篇文章,该文章发生了与成员提供可能解决方案完全相同的问题/sf/answers/3480381411/
我也是这样做的,我已经为Java语句创建了该类型的实时模板,但它仍然不起作用.
有些人对此有任何建议吗?我将感谢你的帮助
提前致谢.

如果我们声明一个变量和一个具有相同名称的函数,则它接受重新声明。但是,当我们在一个块内执行相同的操作时,它会显示重新声明错误。
码:
var x;
function x() {}; // no error.Run Code Online (Sandbox Code Playgroud)
但是在这种情况下,我得到了错误。
{
var inside; // re-declaration error.
function inside() {};
}Run Code Online (Sandbox Code Playgroud)
预期结果应该没有错误。
基本上我在 Node.js 上看过的每个教程,甚至 express 生成器都使用var而不是let? 从我在 Javascript.info 中学到的知识let应该是标准的,除非是非常小众的情况。