学习博客:【JavaScript】内部对象

这篇博客探讨了JavaScript中的内部对象,包括Date对象、JSON数据交换格式的特性,如层次结构清晰、网络传输效率高的优势,以及JSON与JS对象之间的转化。同时,文章还涉及了JavaScript的面向对象概念,特别是类的继承机制,通过原型链实现不同类之间的方法调用。
摘要由CSDN通过智能技术生成

标准对象

typeof 123
'number'
typeof true
'boolean'
typeof [12,1]
'object'
typeof 'a'
'string'
typeof {}
'object'
typeof undefined
'undefined'
typeof alert
'function'
typeof NaN
'number'

Date

var now = new Date();   //当前时间

now.getFullYear();   //年
now.getMonth();   //月
now.getDate();   //日
now.getDay();   //星期几
now.getHours();   //时
now.getMinutes();   //分
now.getSeconds();   //秒

now.getTime();  //时间戳 1970-1-1 00:00:00至今毫秒数
console.log(new Date(14141411515));     //时间戳转为时间
//转换
now = new Date(14141411515);     //时间戳转为时间
now.toLocaleTimeString()
now.toLocaleString()
now.toUTCString()

JSON对象

JSON是一种轻量级的数据交换格式,层次结构简洁清晰,网络传输效率高

对象{ }
数组[ ]
键值对 key : value

JSON字符串和JS对象的转化

var user = {
    name: "yl",
    age: 11,
    sex: '男'
}

//对象转化为json字符串
var jsonUser = JSON.stringify(user)

//json字符串转化为对象
var obj = JSON.parse(jsonUser)    //解析

JSON和JS对象的区别:

{name: 'yl', age: 11, sex: '男'}	//JS对象
'{"name":"yl","age":11,"sex":"男"}'	//JSON字符串

面向对象

me这个类无法调用person类中的方法,只需定义me这个类的原型为person即可调用

var person = {
    name: "杨",
    age: 3,
    run: function (){
        console.log(this.name + "run...");
    }
};

var me = {
    name: "我"
};

//我的原型是人类
me.proto = person;

class继承

1.class关键字在ES6引入

class Student{
    //构造器
    constructor(name){
        this.name = name;
    }

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

var xiaoming = new Student("xiaoming");
var xiaolan = new Student("xiaolan");

2.继承

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

    myGrade(){
        alert("I am a student");
    }
}

var xiaoli = new Person("xiaoli");

本质:查看对象原型

原型链:始终指向Object

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值