一、测试js是单线程:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>js是单线程</title>
<script type="text/javascript">
setTimeout(function () {
console.log("timeout2000");
alert("timeout2000");
},2000);
setTimeout(function () {
console.log("timeout1000");
alert("timeout1000");
},1000);
setTimeout(function () {
console.log("timeout0");
},0);
function fn() {
console.log("fn");
}
fn();
console.log("alert之前");
alert("弹出框");
console.log("alert之后");
</script>
</head>
<body>
</body>
</html>
二、Object构造函数:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Object构造函数</title>
<script type="text/javascript">
var p = new Object();
p = {};
p.name = "李四";
p.age = 21;
p.setName = function (name) {
this.name = name;
}
p.setName("修改后的名字");
console.log(p.name+","+p.age);
</script>
</head>
<body>
</body>
</html>
三、两道js面试题:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>面试题</title>
<script type="text/javascript">
/*
*测试1
*/
function A(){
};
A.prototype.n = 1;
var b = new A();
A.prototype = {n:2,m:3};
var c = new A();
// console.log(b.n);//1
// console.log(b.m);//undefined
// console.log(c.n);//2
// console.log(c.m);//3
/*
*测试2
*/
function F() {
};
Object.prototype.a = function () {
// console.log("a()");//a() a()
}
Function.prototype.b = function () {
// console.log("b()");//b()
}
var f = new F();
f.a();
F.a();
F.b();
// console.log(f);//F{}
// console.log(Object.prototype);
console.log(Function.prototype);//f(){}
</script>
</head>
<body>
</body>
</html>