Javascript面向对象&json(四)

以下是对JavaScript函数与面向对象的学习


内部对象

标准对象

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
typeof 123o
"number"
typeof '123'
"string"
typeof true
"boolean"
typeof NaN
"number"
typeof []
"object"
typeof {}
"object"
typeof Math.abs
"function"
typeof undefined
"undefined"
  1. Date
    基本使用
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    var now = new Date(); //Mon Apr 12 2021 18:27:49 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(1618223269100)); //时间戳转为时间
    转换
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    now = new Date(1618223269100)
    Mon Apr 12 2021 18:27:49 GMT+0800 (中国标准时间)
    now.toLocaleDateString //注意,调用的是一个方式,不是一个属性;
    ƒ toLocaleDateString() { [native code] }
    now.toLocaleString
    ƒ toLocaleString() { [native code] }
    now.toLocaleString()
    "2021/4/12 下午6:27:49"
    now.toGMTString()
    "Mon, 12 Apr 2021 10:27:49 GMT"
  2. json是什么
    早期,所有数据传输习惯使用XML文件
  • JSON是一种轻量级的数据交互格式。
  • 简洁和清晰的层次结构使得JSON成为理想的数据交互语言。
  • 易于人阅读和编写,同时也易于机器解析和生成,并有效地提升网络传输效率。
    在JavaScript一切皆为对象,任何js支持的类型都可以用JSON来表示; number, string…

格式:

  • 对象都用{}
  • 数组都用[]
  • 所有的键值对都是用key:value
  1. JSON字符串和JS对象的转化
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    var student = {
    name: 'zhansan',
    age: 3,
    sex: '男',
    }; //{name: "zhansan", age: 3, sex: "男"}
    //对象转化为JSON字符串 {"name":"zhansan","age":3,"sex":"男"}
    var jasonStudent = JSON.stringify(student);

    //json字符串转化为对象,参数为json字符串
    var obj = JSON.parse('{"name":"zhansan","age":3,"sex":"男"}');
    JSON和js对象的区别
    JSON是一种格式化的字符串,而对象是键值对形式的
    1
    2
    var obj = { a: 'hello', b: 'hellob' };
    var json = '{"a":"hello","b":"hellob"}';

    面向对象编程

    原型对象

  2. 什么是面向对象
    JavaScript,java,c#….面向对象;JavaScript有些区别!
  • 类:模板
  • 对象;具体的实例
    在JavaScript中这个需要大家换一下思维方式!
    原型:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    var student = {
    name: 'zhansan',
    age: 3,
    sex: '男',
    run: function () {
    console.log(this.name + 'run...');
    },
    };
    var xiaoming = {
    name: 'xiaoming',
    };
    //原型对象
    xiaoming.__proto__ = student;
    var Bird = {
    fly: function () {
    console.log(this.name + 'fly....');
    },
    };
    //小明的原型是Student
    xiaoming.__proto__ = Bird;
    1
    2
    3
    4
    5
    6
    7
    function Student(name) {
    this.name = name;
    }
    //给student新增一个方法
    Student.prototype.hello = function () {
    alert('Hello');
    };
  1. class继承
    class关键字,是在ES6引入的
  2. 定义一个类,属性,方法
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    //定义一个学生类
    class Student {
    constructor(name) {
    this.name = name;
    }
    hello() {
    alert(this.name + 'hello');
    }
    }
    var xiaoming = new Student('小明');
    var xioahong = new Student('小红');
    xiaoming.hello();
  3. 继承
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    class Student {
    constructor(name) {
    this.name = name;
    }
    hello() {
    alert('hello');
    }
    }
    class XiaoStudent extends Student {
    constructor(name, grade) {
    super(name); //记得用super调用父类的构造方法!
    this.grade = grade;
    }
    myGrade() {
    alert('我是一名小学生');
    }
    }
    var xiaoming = new Student('小明');
    var xiaohong = new XiaoStudent('小红', 1);
    本质:查看对象原型

查看对象原型

  1. 原型链

原型链

基于B站视频学习 特别感谢up主 遇见狂神说