amandaxcy / error Goto Github PK
View Code? Open in Web Editor NEW我会碰到的错误
我会碰到的错误
setTimeout(function(){
console.log('定时器开始啦')
});
new Promise(function(resolve){
console.log('马上执行for循环啦');
for(var i = 0; i < 10000; i++){
i == 99 && resolve();
}
}).then(function(){
console.log('执行then函数啦')
});
console.log('代码执行结束');
实际输入出结果:
马上执行for循环啦
代码执行结束
执行then函数啦
定时器开始啦
理论:
macro-task(宏任务):包括整体代码script,setTimeout,setInterval
micro-task(微任务):Promise,proc
按照这种分类方式:JS的执行机制是
执行一个宏任务,过程中如果遇到微任务,就将其放到微任务的【事件队列】里
当前宏任务执行完成后,会查看微任务的【事件队列】,并将里面全部的微任务依次执行完
function Foo() {
getName = function () { alert (1); };
return this;
}
Foo.getName = function () { alert (2);};
Foo.prototype.getName = function () { alert (3);};
var getName = function () { alert (4);};
function getName() { alert (5);}
//请写出以下输出结果:
Foo.getName();
getName();
Foo().getName();
getName();
new Foo.getName();
new Foo().getName();
new new Foo().getName();
综合考核变量提升,this指针指向、运算符优先级、原型、继承、全局变量污染、对象属性及原型属性优先级等知
getName()//oaoafly
var getName = function() {
console.log('wscat')
}
getName()//wscat
function getName() {
console.log('oaoafly')
}
getName()//wscat
分解过来就是
var getName
function getName() {
console.log('oaoafly')
}
getName = function() {
console.log('wscat')
}
apply 、 call 、bind 三者都是用来改变函数的this对象的指向的;
apply 、 call 、bind 三者第一个参数都是this要指向的对象,也就是想指定的上下文;
apply 、 call 、bind 三者都可以利用后续参数传参;
bind 是返回对应函数,便于稍后调用;apply 、call 则是立即调用 。
apply、call只是接收参数形式不一样
call(this,arg1,arg2)
apply(this, [arg1, arg2])
function fruits() {}
fruits.prototype = {
color: "red",
say: function() {
console.log("My color is " + this.color);
}
}
var apple = new fruits;
apple.say(); //My color is red
banana = {
color: "yellow"
}
apple.say.call(banana); //My color is yellow
apple.say.apply(banana); //My color is yellow
apple.say.bind(banana)
<script>
var a =1;
function test(){
console.log(a);
var a = 2;
console.log(a);
}
test();
console.log(a);
</script>
输出:undefined,2,1
原因:变量作用域和变量提升的问题
在Javascript中,变量进入一个作用域可以通过下面四种方式:
变量的上升(Hoisting)只是其定义上升,而变量的赋值并不会上升。
function f() { console.log('I am outside!'); }
if(true) {
// 重复声明一次函数f
function f() { console.log('I am inside!'); }
}
f();
上面代码在 'use strict';输出的结果不一样
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.