js常见易错笔试题

var test='hello world';
console.log(this.test);//this指全局window
var test1 = {
test:'hello zhongguo',
func:function(){
return this.test;
}
}
var func = test1.func;
console.log(func()); //如果func=test1.func();则console.log(func);输出为hello zhongguo
//结果:hello world
// hello world

/*var data = ['北京','上海','广州','深圳'];
Array.prototype.getLength = function(){
var length = data.length;
return length;
};
for(var i=0;i<data.length;i++){
console.log(typeof i);//number 0,1,2,3
console.log(data[i]); //打印:北京 上海 广州 深圳
}
for(var i in data){
console.log(i);
console.log(typeof i);//string '1','2','2','3'
console.log(data[i]);//打印:北京 上海 广州 深圳 function(){var length = data.length;return length; }
}
*/
var test = 'shanghai';
function New(){};
console.log(typeof test);// string
console.log(New instanceof Object);// true

var txt='xixi';
function hello(){
var txt;
fn(); //world
var fn=function(){ //这是函数表达式
console.log('hello');
};
function fn(){console.log('world');};
console.log(txt);//undefined 自己作用域内有txt变量则不会去父层找,如果没有则找外层的
fn();// hello
}
hello();
/*其中:var fn=function(){console.log('hello'); };是函数表达式;整个执行过程相当于:
var txt='hx';
function hello(){
var txt;
var fn;
function fn(){console.log('world');};函数声明提前
fn();
fn=function(){console.log('hello');
console.log(txt); 自己作用域内有txt变量则不会去父层找,如果没有则找外层的
fn();
}

打印结果:
world
undefined
hello
*/
/*(function(){
var a=b=5; //a是局部变量 b是在全局作用域下
})();
console.log(a);//报错 :访问不到上面函数内部的局部变量
console.log(b);//结果:5
*/


function test0(){
console.log(a);//undefined
console.log(foo());//2 函数声明提前并执行
var a=1;
function foo(){
return 2;
}
};
test0();
原文地址:https://www.cnblogs.com/qhhw/p/6511160.html