4.函数
4.1定义函数
function abs(x){
if(x>=0){
return x;
}else{
return -x;
}
}
var abs=function(x){
if(x>=0){
return x;
}else{
return -x;
}
}
var abs = function(x){
console.log("x=>"+x);
for (var i = 0; i<arguments.length; i++){
console.log(arguments[i]);
}
if(x>=0){
return x;
}else{
return -x;
}
}
function aaa(a,b, ... rest) {
console.log("a=>"+a);
console.log("b=>"+b);
console.log(rest);
}
4.2变量的作用域
在JavaScript中,var定义变量实际是有作用域的
-
假设在函数体中声明,则在函数体外不可以使用
-
假设在JavaScript中函数查找变量从自身函数开始~,由“内”向“外”查找.假设外部存在这个同名的函数变量,则内部函数会屏蔽外部函数的变量。
-
养成规范:所有的变量定义都放在函数的头部,不要乱放,便于代码维护
function aaa(){
for (let i=0;i<100;i++){
comsole.log(i)
}
comsole.log(i+1);
}
var x = 'xxx';
window.alert(x);
var old_alert = window.alert;
//old_alert(x);
window.alert = function () {
};
// 发现 alert()失效了
window.alert(123);
//恢复
window.alert = old_alert;
window.alert(456);
const PI=3.14; console.log(PI);
4.3方法
方法就是把函数放在对象里面,对象只有两个东西:属性和方法
var sunshen = {
name:'sxc’,
bitrh: 2000,
//方法
age: function () {
// 今年 - 出生的年
var now = new Date().getFullYear();
return now-this.bitrh;
}
}
//属性
sunshen.name
//方法,一定要带()
sunshen.age ()
function getAge() {
// 今年 - 出生的年
var now = new Date().getFullYear();
return now-this.bitrh;
}
var sunshen = {
name:'sxc',
bitrh: 2000,
age: getAge
};
// sunshen. age() ok
getAge.apply(sunshen,[]);// this,指向了 sunshen,参数为空
5.内部对象
5.1 Date
var now = new Date(); //Sat Jan 04 2020 10:47:06 GMT+0800(中国标准时间) now.getFullYear();//年 now.getMonth();//月 0~11代表月 now.getDate(); // 日 now.getDay();// 星期几 now.getHours();//时 now.getMinutes();// 分 now.getSeconds(); // 秒 now.getTime();// 时间戳 全世界统一 1970 1.1 0:00:00 毫秒数 console.log(new Date(1578106175991))//时间戳转为时间
转换
now = new Date(1578106175991)
Sat Jan 04 2020 10:49:35 GMT+0800(中国标准时间)
now.toLocalestring //注意,调用是一个方式,不是一个属性!
f toLocalestring() { [native code] }
now. toLocalestring()
"2020/1/4 上午10:49:35"
now. toGMTString()
"Sat, 04 Jan 2020 02:49:35 GMT"
5.2 JSON
在JavaScript一切皆为对象、任何js支持的类型都可以用JSON来表示;number,string ...
格式:
-
对象都用{}
-
数组都用[]
-
所有的键值对 都是用 key:value
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<script>
var person={
name:"sxc",
age:"22",
sex:"男"
}
//对象转化为json字符串 {"name":"sxc","age":"22","sex":"男"}
var jsonperson=JSON.stringify(person);
//json字符串转化为对象参数为json字符串
var obj=JSON.parse('{"name":"sxc","age":"22","sex":"男"}');
</script>
</body>
</html>
var obj = {a: 'hello',b:'hellob'};
var json = '{"a": "hello","b":"hellob"}'
6.面向对象编程
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<script>
class Student{
constructor(name) {
this.name=name;
}
hello(){
alert('hello')
}
}
var sxc=new Student("sxc");
</script>
</body>
</html>
7.操作BOM对象(重点)
window.alert(1) undefined window. innerHeight 258 window. innerwidth 919 window.outerHeight 994 window. outerwidth 919
screen.width 1920px screen.height 1080px
host:"www.baidu.com"
href:"https://www.baidu.com/"
protocol:"https:"
reload:f reload() // 刷新网页
// 设置新的地址
location.assign('https://blog. kuangstudy.com/')
document.title “百度一下,你就知道”
获取具体的文档树节点
<d1 id="app">
<dt>Java</dt>
<dd>JavaSE</dd>
<dd>JavaEE</dd>
</d1>
<script>
var dl = document. getElementById('app');
</script>
获取cookie
document.cookie '_xsrf=2|8c7b1902|458eb5a7fa1c45676be1cfe9e19cb67e|1700048275'
history.back() //后退 history.forward()//前进










