您的位置:首页 > Web前端 > JavaScript

JavaScript代码基础

2017-11-30 17:42 302 查看
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);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: