- 变量提升
var a = 99; // 全局变量a
f(); // f是函数,虽然定义在调用的后面,但是函数声明会提升到作用域的顶部。
console.log(a); // a=>99, 此时是全局变量的a
function f() {
console.log(a); // 当前的a变量是下面变量a声明提升后,默认值undefined
var a = 10;
console.log(a); // a => 10
}
// 输出结果:
undefined //这里易错,容易和this.a分不开。注意函数也有内部作用域
10
99
关于变量提升问题,对象也属于变量 ,var obj={n:2};如果是console.log(obj),则会因为变量提升问题输出undefined,但如果是console.log(obj.n),则会报错。
- 关于var 和let和settimeout,顺便理解一下settimeout和他的回调函数
https://www.jb51.net/article/122489.htm
for (var i = 0; i <10; i++) {
setTimeout(function() { // 同步注册回调函数到 异步的 宏任务队列。
console.log(i); // 执行此代码时,同步代码for循环已经执行完成
}, 0);
}
// 输出结果
10 共10个
// 这里面的知识点: JS的事件循环机制,setTimeout的机制等
//解决办法,立即执行函数和let
- 判断一个字符串中数量最多的字符,并且输出它
思路:把字符设为对象的属性,多一个字符则属性值加1.
var str = 'asdfssaaasasasasaa';
var json = {};
for (var i = 0; i < str.length; i++) {
if(!json[str.charAt(i)]){
json[str.charAt(i)] = 1;
}else{
json[str.charAt(i)]++;
}
};
var iMax = 0;
var iIndex = '';
for(var i in json){
if(json[i]>iMax){
iMax = json[i];
iIndex = i;
}
}
console.log('出现次数最多的是:'+iIndex+'出现'+iMax+'次');
- 如下代码的结果:关于函数作用域局部变量和变量提升的问题。
var name="World!";
(function(){
var name;//因为自调函数特殊,具有闭包,所以可以看作一个独立的作用域。所以是undefined。
if(typeof name=== 'undefined'){
name='Jack';
console.log('Goodbye'+name);
}
else
{
console.log('hello'+name);
}
}
)();
//Goodbye jack
console.log(name); //undefined;js是弱语言,无需声明就可以使用
(function(){
var name; //因为自调函数特殊,具有闭包,所以可以看作一个独立的作用域。
if(typeof name=== 'undefined'){
name='Jack';
console.log('Goodbye'+name);
}
else
{
console.log('hello'+name);
}
}
)();
//报错,
console.log(name);
var name;
//undefined;//有变量声明;变量提升,注意和上面的区别。
- 函数也有整体变量声明。这里容易和普通函数的全局环境混,全局环境指的是函数内部可以使用全局的变量,但是全局不能使用函数内部的变量呀。
console.log(f1); //函数提升,整个代码块提升到文件的最开始<br> 输出f1函数
f1(); //我是函数f1
console.log(f2);//undefined,因为字面量声明的函数无法变量提升
function f1(){
console.log('我是函数f1。。。');
}
var f2 = function () {
console.log('我是函数f2。。。');
};
(function () {
console.log('closure');
a = 'aaa';
var a = 'bbb';
console.log(a); //bbb,局部变量bbb,全局变量aaa
})();
- (function() {
var a = b = 5;
})();
console.log(b);//5,因为执行顺序b=6,var a=b;自执行后b为全局变量
console.log(a);//a is notdefined因为a为局部变量,所以函数结束后就自动销毁; - 用jquery实现进度条的想法,首先是页面先加载。因为在页面加载好我们才会调用load()方法里执行的函数,所以在函数外定义一个定时器,规定好我们预定加载的时长,如果时长大于我们预先设定好的加载时间,就调用这个定时器显示加载时间过长需要等待。(比较麻烦,网上找一个随便练习一下就好。)
var widthcount = 0;
var max = parseInt(Math.random() * 8 + 90);
var time = parseInt(Math.random() * 60 + 40);
var timer = window.setInterval(function() { //这个是在页面正在加载的时候设置进度条
time = parseInt(Math.random() * 60 + 40);
loadingFun();
}, time);
setTimeout(function(){
if(max<100){
confirm("网络加载较慢,请耐心等待!");
}
},10000);
还有当我们页面加载好,但是导航条进度没有满的时候,我们需要在load()里把之前添加的定时器去掉,重新添加一个定时器把没有完成的进度条再完成。完成后进入下一页面。
代码如下:
$(window).load(function() {
// 背景音乐
// $('#musicfx').get(0).play();
window.clearInterval(timer);
max = 100;
var timer2 = setInterval(function() {
loadingFun();
}, 50);
var timer3 = setInterval(function() {
if(widthcount >= 100) { //如果进度条满了,则可以进入页面里。
window.clearInterval(timer2);
$(".loading").hide();
$(".container").show();
// 开学畅想几个字浮出
$(".kaixue-word").addClass("show");
// 背景音乐
//$('#musicfx').get(0).play();
}
}, 100);
});
- promise.all的应用,以及了解一下promise异步是如何使用的(在then函数里放入一个函数,函数添加then函数处理),多敲一敲
created(){
this.init()
},
methods: {
init () {
this.$store.dispatch('setLoading', true)
const Ajax = new Promise((resolve, reject) => {
getAllRecruiter().then(res => {
this.recruiter.option = res.data
this.recruiter.val = res.data[0]
resolve(res) // 请求成功后,利用resolve改变promise状态,并且作为返回值传回到promise对象
}).catch((err) => {
reject(err); // 请求失败则利用reject改变状态,终止当前函数流
})
})
const Ajax0 = new Promise((resolve, reject) => {
getAllEmploymentType().then(res => {
this.employmentType.option = res.data
resolve()
}).catch(() => {
})
})
const Ajax1 = new Promise((resolve, reject) => {
getAllLevel().then(res => {
this.careerLevel.option = res.data
resolve()
}).catch(() => {
})
})
const Ajax2 = new Promise((resolve, reject) => {
getAllBenefit().then(res => {
this.benefits.option = res.data
resolve()
}).catch(() => {
})
})
Promise.all([Ajax, Ajax0, Ajax1, Ajax2]).then(() =>
{
//所有请求都成功后执行函数
this.getOldData()
}).catch(() => {
异常处理
this.$store.dispatch('setLoading', false)
})
}
}
- 一些常用的event属性
<script type="text/javascript">
window.onload = function() {
document.getElementById("main").onclick = function(e) {
console.log(e.target);//<div id="main" class="sb js node"><span>测试文字</span></div>
console.log(e.target.id);//main
console.log(e.target.tagName);//DIV
console.log(e.target.nodeName);//DIV
console.log(e.target.classList);// ["sb", "js", "node", value: "sb js node"]
console.log(e.target.className);//sb js node
console.log(e.target.innerHTML);//<span>测试文字</span>
console.log(e.target.innerText);//测试文字
}
}
</script>
</head>
<body>
<div id="main" class="sb js node"><span>测试文字</span></div>
</body>
var f = function g() {
return 23;
};
typeof g(); //报错
如果是typeof f,结果是function,因为是将函数付给了f
如果是typeof f(),结果是number,f是函数,f()是调用函数,输出的是结果,结果为number
如果是typeof g,结果是undefined.
如果是typeof g(),结果是ReferenceError,g is not defined
关于最后两个,类似于 var foo = function bar(){...} 这样的东西统一按 2 方法处理,即在函数外部无法通过 bar 访问到函数,因为这已经变成了一个表达式。
但为什么不是 "undefined"?这里如果求 typeof g ,会返回 undefined,但求的是 g(),所以会去先去调用函数 g,但g已经无法通过其访问了,所以会报错,
var foo=function(x,y){
return x-y;
}
function foo(x,y){
return x+y;
}
var num=foo(1,2); //-1
因此函数存在变量提升的问题,所以function foo会被提升到顶部,先执行,再执行var foo,因为变量式声明没有变量提示
代码var foo = "10"+3-"1";console.log(foo);执行后,foo的值为( ) //102
对于“+”来说,有两个含义:第一个含义是做字符串拼接,第二个含义是加减法中的加法。
1,如果操作数里有一个是字符串,其他的值将被转换成字符串;
2,其他情况,操作数转换成数字执行加法运算。
而对于“ - ”来说,只有一个含义,就是做减法,自然不会转化成字符串了。
- 判断字符中的大小写,用正则表达式(熟悉一下正则表达式)