Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Javascript比较var,let,const #11

Open
pengjielee opened this issue Jun 24, 2019 · 0 comments
Open

Javascript比较var,let,const #11

pengjielee opened this issue Jun 24, 2019 · 0 comments

Comments

@pengjielee
Copy link
Owner

pengjielee commented Jun 24, 2019

1、var声明

//demo11: 区域变量覆盖全局变量

var name = 'jim';
function f(){
	console.log(name);
	if(true){
		var name = 'tom'
	}
}
f(); // undefined

//demo11.1

var name = 'jim';
function f(){
	console.log(window.name);
	console.log(name);
	if(true){
		var name = 'tom'
	}
}
f(); // jim undefined

//demo11.2

var name = 'jim';
function f(){
	console.log(name)
	if(true){
		var name = 'tom'
	}
	console.log(name);
}
f(); // undefined tom

//demo12: 循环变量泄漏为全局变量

var str = 'hello';
for(var i=0; i < str.length; i++){
	console.log(str[i]);
}
console.log(i);

2、let/const

//demo21

const name = 'hello'

function f(){
	console.log(name);
	if(true){
		const name = 'world'
		console.log(name)
	}
}
f();

output: 
hello 
world

//demo22

const str = 'hello';
for(let i=0; i < str.length; i++){
	console.log(str[i]);
}
console.log(i); // Uncaught ReferenceError: i is not defined
@pengjielee pengjielee changed the title Javascript中的var,let,const Javascript比较var,let,const Jun 24, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant