JavaScript --- Object

JavaScript object is a container attribute variables

1. Internal Objects

Standard vertebral objects

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

Basic use

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)) //时间戳转为时间

Change

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

What is json

Early on, all the data transmission accustomed to using XML files!

  • The JSON ( the JavaScript Object Notation, the JS objects notation) is a lightweight data-interchange format.
  • Simple and clear hierarchy make JSON an ideal data-interchange language.
  • Easy to read and write, but also easy for machines to parse and generate, and effectively improve the efficiency of network transmission.

In JavaScript everything is an object, any type of support js can use JSON to represent; number, string ...

format:

  • Object with {}
  • Arrays are used []
  • All key-value pairs are used key: value

Conversion JSON string object and 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":"女"}');

A lot of people do not know the difference, JSON and JS objects

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

3.Ajax

  • Js native asynchronous request written xhr
  • Method jQuey packaged $ ( "# name"). ajax ( "")
  • axios request

2. Object-Oriented Programming

Prototype object

javascript, Java, c #. . . . Object-oriented; javascript some differences!

  • Class: template prototype object
  • Object: Specific examples

In JavaScript this need to change my way of thinking!

prototype:

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 inheritance

classKeyword, introduced in ES6

1, the definition of a class, property, method

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

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

    hello(){
        alert('hello')
    }

}

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

2, inheritance

<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>

Nature: View object prototype
__ proto __:
Here Insert Picture Description

Published 39 original articles · won praise 1 · views 536

Guess you like

Origin blog.csdn.net/love_to_share/article/details/103927795