追根溯源

arr的输出情况

<script>
    // 1、arr的输出
    var t1 = [4,5,5,6,5,7];
	console.log(t1); //[4, 5, 5, 6, 5, 7]
	console.log("+++" + t1); // +++ 4,5,5,6,5,7
	//CON 和字符串一起打印时,数组会打印每一个值,而且带上逗号
	
	var t2 = [];  
	console.log(t2); // []
	console.log("+++" + t2); //+++
	
	var t3 = [,,,,,]; 
	console.log(t3); // [empty × 5]
	console.log("+++" + t3);  //+++,,,,
	//CON 若声明数组时用了",",则数组拥有了相应的length,并且和字符串一起打印时会打印逗号
	
	var t4 = [1,2,4,5,6];
	var t5 = t4.map(function(item){ 
		console.log("I am in map");
	});
	console.log(t5); //[undefined, undefined, undefined, undefined]
	console.log("+++" + t5); //+++,,,
	//map()参数如果没有返回值,它创建的新数组类似t3,有length,而不是像t2
	
	// t2,t3,t5的区别
	console.log("-----length");
	console.log(t2.length); // 0
	console.log(t3.length); // 5
	console.log(t5.length); // 5
	console.log("-----[1]");
	//CON []的length为0,而[,,,,,]和t5都有length,且为5
	
	//相同点
	console.log(t2[1]); // undefined
	console.log(t3[1]); // undefined
	console.log(t5[1]); // undefined
	
	// 函数调用
	console.log("----函数调用")
	function fn(arr){
		console.log(arr);
	}
	fn(t2);  //[]
	fn(t3);  //[empty × 5]
	fn(t5); //[undefined, undefined, undefined, undefined, undefined]
	//CON 它们都可以作为函数参数传递,并且不会报错,因为它们都是已经声明了的变量
</script>

null 和 undefined 的区别

<script>
    var a = null;
    var b = undefined;
    
    console.log(a); // null
    console.log(b); // undefined
    console.log(typeof(a));  //object
    console.log(typeof(b));  //undefined
    //con: null是一个对象,而undefined就表示一个数据类型,跟object,NUM等并列
    
    function fn(t){
    	console.log(t);
    }
    fn(a); //null
    fn(b); //undefined
    /*fn(c); //会报错 c is not defined */
   //con: 两者作为函数参数传递时不会报错,所以它们跟没有声明过的变量(c)是有区别的,
   //     它们已经是声明了的变量的一个值,所以它们在内存栈中已经有对应的位置了
   
   a = {};
   console.log(a); // {}
   a.b = 8;
   console.log(a.b); // 8
   //con 当a = null时,a.b = 8会报错,若想为a设置属性b必须先执行 a = {};但是a = null时作为函数实参又是可以的,不会报错
</script>

this 和 prototype

function Cat(nameA,num){
		this.nameA = nameA;
		this.num = num;
	}
	Cat.prototype.age = 15;
	Cat.prototype.napName = 'puppyCat';
	Cat.prototype.run = function(){
        console.log(this.age); // 15	  
        console.log(this.name); // cat1
		
		console.log(this.num); // 11
		console.log(this.napName); // puppyCat
			
		console.log(name);  // name是一个空白字符串类型
		// console.log(nameA);  // nameA is not defined at Cat.run
		//console.log(num); // num is not defined at Cat.run
		//CON 原型上的方法若调用属性不加this时 会报错
		// 注意name这个特殊的名字,它应该是prototype本身自带的一种属性,所以即使不加this也不会报错
		
		//console.log(age); // age is not defined at Cat.run	
    	//console.log(napName); //napName is not defined at Cat.run
    	//CON:原型上的方法不能调用原型上的添加的属性 会提示 not defined
	}
posted @ 2020-04-14 21:00  Mrzhozho  阅读(40)  评论(0)    收藏  举报