我对es6 let
和const
之间的差异非常熟悉。我不太确定的是在for..of
循环的定义范围内。
我知道一个"老派" for循环签名不接受const
即
for (let i = 0; i < 100; i++) ... // works in chrome
for (const i = 0; i < 100; i++) ... // does not work in chrome (reassignment error)
然而,使用for..of
循环,它似乎没有什么区别
const someArray = ['hello', 'hey'];
for (let elem of someArray) ... // works
for (const elem of someArray) ... // also works?
那么这里发生了什么?
为什么const
允许for..of
而不是旧学校for循环?
循环中的最终结果差异是什么? (除了可能在循环中重新分配elem
,我不会期望很多人做的事情)
答案 0 :(得分:5)
只重新分配一个变量,因此错误
if (!IsPostBack)
{
DataTable dt = new DataTable();
DataRow dr;
dt.Columns.Add("sno");
dt.Columns.Add("itemname");
dt.Columns.Add("quantity");
dt.Columns.Add("price");
dt.Columns.Add("totalprice");
dt.Columns.Add("image");
if (Request.QueryString["itemname"] != null)
{
if (Session["Buyitems"] == null)
{
dr = dt.NewRow();
SqlConnection scon = new SqlConnection(ConfigurationManager.ConnectionStrings["online food orderingConnectionString"].ConnectionString);
scon.Open();
String myquery = "select * from food_items where item_name=@items_name";
SqlCommand cmd = new SqlCommand(myquery, scon);
cmd.Parameters.AddWithValue("items_name", Request.QueryString["itemname"].ToString());
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
dr["sno"] = 1;
if (ds.Tables[0].Rows.Count > 0)
{
dr["itemname"] = ds.Tables[0].Rows[0]["item_name"].ToString();
dr["image"] = ds.Tables[0].Rows[0]["image"].ToString();
dr["price"] = ds.Tables[0].Rows[0]["price"].ToString();
int price = Convert.ToInt16(ds.Tables[0].Rows[0]["price"].ToString());
int quantity = Convert.ToInt16(Request.QueryString["quantity"].ToString());
int totalprice = price * quantity;
dr["quantity"] = Request.QueryString["quantity"];
dr["totalprice"] = totalprice;
SaveCartDetail(ds.Tables[0].Rows[0]["item_name"].ToString(), Request.QueryString["quantity"], ds.Tables[0].Rows[0]["price"].ToString(), totalprice.ToString());
dt.Rows.Add(dr);
GridView1.DataSource = dt;
GridView1.DataBind();
Session["buyitems"] = dt;
GridView1.FooterRow.Cells[4].Text = "Total Amount";
GridView1.FooterRow.Cells[5].Text = grandtotal().ToString();
}
}
}
}
private void SaveCartDetail(String itemname, String quantity, String price, String totalprice)
{
String query = "insert into cart(item_name, quantity, price, totalprice, username) values ('" + itemname + "','" + quantity + "','" + price + "','" + totalprice + "','" + Session["username"].ToString() + "')";
SqlConnection scon1 = new SqlConnection(ConfigurationManager.ConnectionStrings["online food orderingConnectionString"].ConnectionString);
scon1.Open();
SqlCommand cmd1 = new SqlCommand(query, scon1);
cmd1.ExecuteNonQuery();
scon1.Close();
Response.Write("Items saved in cart");
}
为每个循环创建了一个单独的变量,因此工作正常
for (const i = 0; i < 100; i++)