JavaScript---对象

JavaScript 对象是属性变量的容器

1.内部对象

标椎对象

typeof 123
"number"
typeof '123'
"string"
typeof true
"boolean"
typeof NaN
"number"
typeof []
"object"
typeof {}
"object"
typeof Math.abs
"function"
typeof undefined
"undefined"

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(1578647754614)
Fri Jan 10 2020 17:16:29 GMT+0800  (中国标准时间)
now.toLocaleString // 注意,调用是一个方式,不是一个属性!
ƒ toLocaleString() { [native code] }
now.toLocaleString()  
"2020/1/10 下午17:16:29"
now.toGMTString()
"Fri Jan 10 2020 17:16:29 GMT+0800 "

2.JSON

json 是什么

早期,所有数据传输习惯使用 XML 文件!

  • JSON(JavaScript Object Notation, JS 对象简谱) 是一种轻量级的数据交换格式。
  • 简洁和清晰的层次结构使得 JSON 成为理想的数据交换语言。
  • 易于人阅读和编写,同时也易于机器解析和生成,并有效地提升网络传输效率。

在JavaScript 一切皆为对象、任何js 支持的类型都可以用JSON来表示;number,string…

格式:

  • 对象都用 {}
  • 数组都用 []
  • 所有的键值对 都是用 key:value

JSON字符串 和 JS 对象的转化

var user = {
    name: "yy",
    age: 18,
    sex: '女'
}

//对象转化为json字符串 {"name":"yy","age":18,"sex":"女"}
var jsonUser =  JSON.stringify(user);

//json 字符串转化为对象 参数为 json 字符串
var obj = JSON.parse('{"name":"yy","age":18,"sex":"女"}');

很多人搞不清楚,JSON 和 JS 对象的区别

var obj = {a: 'hello',b:'hellob'};
var json = '{"a": "hello","b":"hellob"}'

3.Ajax

  • 原生的js写法 xhr 异步请求
  • jQuey 封装好的 方法 $(”#name“).ajax(“”)
  • axios 请求

2.面向对象编程

原型对象

javascript、Java、c#。。。。面向对象;javascript有些区别!

  • 类: 模板 原型对象
  • 对象: 具体的实例

在JavaScript这个需要大家换一下思维方式!

原型:

var Student = {
    name: "yy",
    age: 18,
    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;
function Student(name) {
    this.name = name;
}

// 给student新增一个方法
Student.prototype.hello = function () {
    alert('Hello')
};

class 继承

class关键字,是在ES6引入的

1、定义一个类,属性,方法

// 定义一个学生的类
class Student{

    constructor(name){
        this.name = name;
    }

    hello(){
        alert('hello')
    }

}

var xiaoming = new Student("xiaoming");
var xiaohong = new Student("xiaohong");
xiaoming.hello()

2、继承

<script>

    //ES6 之后=============
    // 定义一个学生的类
    class Student{

        constructor(name){
            this.name = name;
        }

        hello(){
            alert('hello')
        }
    }

class XiaoStudent extends Student{
    constructor(name,grade){
        super(name);
        this.grade = grade;
    }

    myGrade(){
        alert('我是一名小学生')
    }

}

var xiaoming = new Student("xiaoming");
var xiaohong = new XiaoStudent("xiaohong",1);


</script>

本质:查看对象原型
__ proto __:
在这里插入图片描述

发布了39 篇原创文章 · 获赞 1 · 访问量 536

猜你喜欢

转载自blog.csdn.net/love_to_share/article/details/103927795