当前位置:首页 > 技术分享 > 正文内容

JavaScript 05

冰凉2022-09-30 19:55技术分享0

方法

定义方法

方法就是把函数放在对象里面,对象只有两个东西 属性和方法

ar cold = {
    name:'yc',
    birth:"2005",
    age:function(){
        var now = new Date().getFullYear();
        return now - this.birth;
    }
    }
    
    //调用属性直接
    cold.name
    //调用方法
    cold.age()

拆分出来

function getAge(){

                var now = new Date().getFullYear();
                return now - this.birth;
            }
        var cold1 = {
            name:'yc',
            birth:"2005",
            age:getAge
            }

apply

在js中可以控制this的指向

function getAge(){
        var now = new Date().getFullYear();
        return now - this.birth;
        }

        var cold1 = {
        name:'yc',
        birth:"2005",
        age:getAge
        }
        getAge.apply(cold1,[])//this 指向了 cold1

对象

标准对象

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

Date

基本用法

var now = new Date();
        now.getFullYear();//年
        now.getMonth();//月份
        now.getDate();//日
        now.getDay();//星期几
        now.getHours();//时间
        now.getMinutes();//分钟
        now.getSeconds();//秒
        now.getTime();//时间戳
        new Date(1664532273706)
     //Fri Sep 30 2022 18:04:33 GMT+0800 (GMT+08:00)

转换

new Date(1664532273706)



now.toLocaleDateString();//获取本机时间
 //'2022/9/30'
 now.toGMTString();
 //'Fri, 30 Sep 2022 10:04:33 GMT'

JSON


  • 对象{}

  • 数组[]

  • 所有的键值对 都用key:value

JSON字符串和js对象的转化

//定义js对象
        var user = {
            name:"cold",
            age:"18",
            sex:"男"
        }
        //对象转为JSON类型
        var jsonUser = JSON.stringify(user);
        '{"name":"cold","age":"18","sex":"男"}'
        //json类型转换为对象
        var obj = JSON.parse(jsonUser);

JSON和js对象的区别

//对象
{name: 'cold', age: '18', sex: '男'}
//JSON对象
'{"name":"cold","age":"18","sex":"男"}'

面向对象编程

原型

var student = {
name:"cold",
age:18,
run:function(){
    console.log(this.name + "正在敲代码.....")
}
};
var cold = {
name:'coldd'
}
//原型对象
//cold 的原型就是student
cold.__proto__ = student;
var xiaoniao = {
// name:'coldd',
youyong:function(){
    console.log(this.name +"正在游泳.....")
}
}

class 继承

//定义一个类
class student1{
constructor(name){
    this.name = name;
}
hello(){
    alert('hello')
}
}

var cold = new student1("coldd666")
//cold.hello()

实现继承

//定义一个类
class student1{
constructor(name){
    this.name = name;
}
hello(){
    alert('hello')
}
}


class student2 extends student1{
constructor (name,age){
    super(name)
    this.age = age;
}
daxuesheng(){
alert('我是一个大学生')
}
}

var cold = new student1("coldd666")
//cold.hello()
var cold1 = new student2("coldd666",22)


版权声明:本文由AZPL工作室发布,如需转载请注明出处。

笔记本交流群:1025638188

本文链接:https://www.azplstudio.top/post/55.html

分享给朋友:

“JavaScript 05” 的相关文章

笔记本驱动安装教程!

笔记本驱动安装教程!

笔记本难免会出现各种问题,扬声器不响了、显卡不动了、麦克风没声音了等等,这些情况说大,跑去售后又有点小题大做,说小,对于新手不太容易,今天这篇文章就来教教大家如何刷笔记本的驱动及Bios。根据你的笔记...

50天50个案例02

50天50个案例02

<!DOCTYPE html> <html lang="en"> <head>    &...

源码提供

源码提供

此分区的所有代码已上传至:https://github.com/BLovo22/shop  供下载,浏览。...

发表评论

访客

◎欢迎参与讨论,请在这里发表您的看法和观点。