JavaScript代碼基礎

1、typeof操作

typeof的返回值爲字符串

typeof null;//"object"
typeof undefined;//"undefined"
typeof NaN;//"number"
typeof 1;//"number"
typeof Number.Min_VALUE;//"number"
typeof Infinity;//"number"
typeof "123";//"string"
typeof true;//"boolean"
typeof window;//"object"
typeof document;//"object"
typeof eval;//"function"
typeof Date;//"function"
typeof Array;//"function"
typeof Math;//"object"
typeof a;//"undefined"

null instanceof Object;//false
2、字符串操作

undefined + NaN => NaN
“123” + NaN => “123NaN”
NaN + “undefined” => “NaNundefined”
true + 1 => 2
“3” + 0 => 30//只要有一邊爲字符串,使用"+"時就會進行字符串的拼接
undefined + 11 => NaN
isNaN(true) => false
isNaN(false) => false
null == undefined => true
null === undefined => false
[1,2,3] === [1,2,3] => false

var str = "";
if(a == undefined){
	str = "1";
}else{
	str = "2";
}
console.log(str);//1
if(typeof a == undefined){
	str = "3";
}else{
	str = "4";
}
console.log(str);//4
if(typeof a == "undefined"){
	str = "5";
}else{
	str = "6";
}
console.log(str);//5
3、函數操作

   1>、函數的解析順序

//函數一:
var foo = 1;
(function(){
	console.log(foo);//undefined
	var foo = 2;
	console.log(foo);//2
})();
//函數二
var a = 1;
function foo(){
	var a = 2;
	var b = 3;
	console.log(b);
	c = 4;
}
console.log(a);//1
foo();//3
console.log(c);//4

   2>、屬性操作delete

  在使用delete操作符時,可以刪除沒有使用var聲明的變量,可以刪除對象的屬性;但是不是刪除傳入的參數

(function(){
	delete x;
	console.log(x);//6
})(1+5);

   3>、this指向

var x = 3;
var foo = {
	x: 2,
	baz: {
		x: 1,
		bar: function(){
			return this.x;
		}
	}
}
var a = foo.baz.bar;//返回值爲一個函數
a();//3,調用這個函數時,是window對象來調用,所以this指向window,返回值爲3
foo.baz.bar();//1,在調用bar函數時,使用foo.baz對象來調用的,所以this指向foo.baz,所以返回值爲1

   4>、變量提升

//在函數執行if判斷時,name處於聲明狀態還未賦值,此時typeof name的值爲“undefined”,然後再賦值,所以輸出“John”
var name = "Jack";
(function(){
	if(typeof name === "undefined"){
		var name = "John";
		console.log(name);//John
	}else{
		console.log(name);
	}
})();

   5>、arguments參數

function setArr(arr){
	arr[0] = arr[2];
}
function getArr(a, b, c){
	c = 10;
	setArr(arguments);
	return a + b + c;//a=10,b=1,c=10
}
getArr(1, 1, 1);//21

   6>、變量的聲明

(function(){
	var x = y = 1;//var x=1; y=1;y相當於是一個全局變量
})()
console.log(y);//1
console.log(x);//x is not defined

   7>、for循環

var k = 0;
for(var m=0,n=0;m<10,n<7;m++,n++){
	k = m + n;
}
console.log(k);//輸出結果爲12
var k = 0;
for(var m=0,n=0;m<7,n<10;m++,n++){
	k = m + n;
}
console.log(k);//輸出結果爲18

   8>、定義一個函數:function repeat(func,times,wait){…}這個函數能夠返回一個新函數,比如這樣用var repeatedFun = repeat(alert,10,5000),調用這個repeatedFun(“helloworld”),會alert10次,每次間隔5秒

function fun(str){
	console.log(str);
}
function repeat(func, times, wait){
	return function(str){
		var timer = setInterval(function(){
			func(str);
			times--;
			if(times <= 0){
				clearInterval(timer);
			}
		},wait);
	}
}
var repeatedFun = repeat(fun, 10, 5000);
repeatedFun("hello world");

下邊這兩個函數參考地址:https://www.cnblogs.com/wgdong/p/5288598.html

function repeat(func, times, wait){
	function repeatFun(){
		var handle,
			_arguments = arguments,
			i = 0;
		handle = setInterval(function(){
			i = i + 1;
			if(i === times){
				clearInterval(handle);
				return;
			}
			func.apply(null, _arguments);
		},wait);
	}
	return repeatFun;
}
var repeatF = repeat(alert, 10, 5000);
repeatF("hello world");
function repeatFun(fn, times, wait){
	var i = 0;
	var handle = setInterval(function(){
		fun.apply(null, arguments);
		if(i == 9){
			clearInterval(handle);
		}
		i++;
	},wait);
}
function fun(){
	console.log("hello world");
}
repeatFun(fun, 10, 5000);
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章