当前位置:网站首页>ES6 adds (I) let and constant
ES6 adds (I) let and constant
2022-07-20 12:17:00 【Guo Guo Han Han】
let And var The difference between
- let Have a scope , If let stay {} Inside curly brackets , Then his scope is limited to curly braces , An error will be reported when output outside .
let Cannot repeat declaration ,var The repeated declaration of will overwrite the previous var, however let Can't , When you declare a variable twice , The system will also give you an error .
- let No variable Promotion , Variable promotion is similar to
var i = 10; Equivalent to var i;i=10; And it will var i; Automatically promote to the front
however let No, , If
consol.log(i);
let i = 10;
The system will also give you an error , If you use var, The system will not give you an error ,
console.log(i);var i = 10;
At this time, the output value of the console is undefined, This is because the code at this time is equivalent to
var i;console.log(i);i = 10
4.let There is a temporary deadband
let man = "123456";
{
console.log(man);// In a dilemma , So wrong reporting
let man = "654321"
}
console.log(man);
The rule of constants :
Constants have and let Similar rules , namely , Constants have their own scope 、 Cannot repeat declaration 、 No variable Promotion 、 Temporary deadband , besides , It is better than let Another rule is that you can't change your value , as follows
const i = 10;
i = i+10;
console.log(i);// Report errors , Because constants generally cannot be modified
The system will directly report an error .
Next, do some exercises :
- Use the cycle output button , When you click each button , He will pop up the button of which output he is
- Because the constant is ES6 newly added , that ES6 How to make a variable constant before
1.
for(let i = 1;i <= 10; i++){
let btn = document.createElement("button");
btn.innerText = i;
btn.onclick = function(){
alert(i)
console.log(i)
}
document.body.appendChild(btn);
}
without let Writing will be troublesome , The output is 10, however let It will make the whole process simple .
2.
Object.defineProperty(Object,'freezeObj',{
value:function(obj){
for (var i in obj) {
if (obj.hasOwnProperty(i)) {
Object.defineProperty(obj,i,{
writable:false
})
}
}
Object.seal(obj);
}
});
var xiaoming = {
age:18,
name:" Zhang San ",
function(){
console.log("123456")
}
}
Object.freezeObj(xiaoming)
边栏推荐
猜你喜欢
Dynamic ring monitoring module, dynamic ring monitoring module classification
kvm部署及应用
DOM series change element content
LabVIEW在同一个面板下描绘模拟波形和数字波形
DOM系列之DOM介绍及获取
DeFi 2.0的LaaS协议Elephant,重振DeFi赛道发展的关键
Opencv (12): cv:: rectangle learning and code demonstration, using OpenCV to draw rectangles / rectangular boxes
MySQL学习笔记——存储过程和函数
Cloud Foundry 开发者之课程 (LFD232) 3. core concepts
The LAAS protocol elephant of defi 2.0 is the key to revitalizing the development of defi track
随机推荐
ES6 exercise
Dap+esb data warehouse construction process description
流量控制系统pid整定方法仿真
高斯数学——看动画学奥数
恢复360收藏夹方法 电脑损坏、重装系统后,把360sefav_new_2021_07_16.favdb这种文件,复制到别的电脑上,含有字符new的文件是加密的无法恢复
The LAAS protocol elephant of defi 2.0 is the key to revitalizing the development of defi track
DOM系列之排他思想
Favorite address and historical address of 360 browser
Object. Defineproperty, callback function, array method
故障006:连接排序去重结果不如人愿
植物叶片叶绿素测定
如何来反向论证产品的好坏?
20220710 leetcode week: move the clip to get the string
DOM系列之样式属性操作
Modeling and Simulation of DC speed regulation system based on Fuzzy PID control
Methods of querying Oracle11g logs, database audit, record troubleshooting
源码编译安装LAMP
Shell function array job
Yunna - dynamic environment monitoring inspection table, information machine room inspection table
Restore 360 favorites method after the computer is damaged and the system is reinstalled, put 360sefav_ new_ 2021_ 07_ 16. Favdb files are copied to other computers. Files containing the character new