![在这里插入图片描述](https://img-blog.csdnimg.cn/20191010155922814.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3UwMTAzNTY3Njg=,size_16,color_FFFFFF,t_70)
打印屏幕和网页宽高
console.log(window.screen.width);
console.log(window.screen.height);
console.log(document.documentElement.clientWidth);
console.log(document.documentElement.clientHeight);
单击按钮关闭浏览器
function exit(){
//window是bom对象
//div,input等是dom对象
var isExit = window.confirm("确定要退出吗?");
if(isExit){
window.close();
}
}
<input type="button" onclick="exit()" value="button">
延迟执行
var index = 0;
function run(){
index++;
console.log("run index:"+index);
}
//5s后执行run
window.setTimeout(run,5000);
console.log("window setTimeout");
先打印了window setTimeout,5s后执行了run()
每隔1s执行
<html lang="en">
<head>
......
<title>Document</title>
<script src="main.js" type="text/javascript"></script>
</head>
<body>
<input type="button" onclick="stop()" value="button">
</body>
</html>
var index = 0;
function run(){
index++;
console.log("run index:"+index);
}
//每隔1s执行1次run
var thread = window.setInterval(run,1000);
function stop(){
window.clearInterval(thread);
}
每隔1s执行run,单击button停止
注意,js中不能实现多线程
js_74">js中的面向对象
直接创建对象
var object = new Object();
object.name = "宝强";
object.age = 18;
object.show = function(){
window.alert(this.name+","+this.age);
}
object.show();
json_86">通过json创建对象
var userEntity = {"name":"宝强","age":18};
userEntity.show = function(){
window.alert(this.name+","+this.age);
}
userEntity.show();
通过函数创建对象
//UserEntity是类还是函数
function UserEntity(name,age){
this.show = function(){
window.alert(this.name);
}
}
var user1 = new UserEntity();
user1.name = "宝强";
console.log(user1.name);
user1.show();
//通过类能创建多个对象
var user2 = new UserEntity();
user2.name = "小宋";
console.log(user2.name);
执行效果和第一个是相同的