首页 > 编程语言 >[JavaScript-06]面向对象

[JavaScript-06]面向对象

时间:2022-11-10 12:11:45浏览次数:43  
标签:06 log author JavaScript console 面向对象 Book year title

1. 面向对象

// 大括号就是对象
p =
{
    a:1,
    b:2,
    c:3,
}

// 系统对象全部基于window
// console.log(window);
// window.alert(1);
// alert(2);
// console.log(navigator.appVersion);

// 对象Object
const book1 =
{
    title   : 'Book One',
    author  : 'John',
    year    : '2012',
    getSummary: function()
    {
        return `${this.title} was written by ${this.author} in ${this.year}`
    }
}
console.log('book1:',book1);
console.log('book1.title:',book1.title);
console.log('p:',p);


const book11 =
{
    title   : 'Book Two',
    author  : 'Tom',
    year    : '2016',
    getSummary: function()
    {
        // this 这个对象
        return `${this.title} was written by ${this.author} in ${this.year}`
    }
}
console.log('book11:',book11);
console.log('book11.getSummary():',book11.getSummary());
// 对象所有的值
console.log(Object.values(book11));
// 对象所有的键名
console.log(Object.keys(book11));

2. 构造函数

/**
构造函数的特点
1. 首字母必须大写
2. 内部使用this对象,来指向即将要生成的实例对象
3. 使用new来生成实例对象
**/
function Book1(title,author,year)
{
    this.title = title;
    this.author = author;
    this.year = year;
}
const book11 = new Book1('Book One','John','2012');
const book12 = new Book1('Book Two','Tom','2016');

console.log('book11.year:',book11.year);
console.log('book11:',book11);

// 构造函数2 
// 问题类中的方法通过原型prototype全部展现
function Book2(title,author,year)
{
    this.title = title;
    this.author = author;
    this.year = year;
    this.getSummary= function()
    {
        return `${this.title} was written by ${this.author} in ${this.year}`
    }
}

const book21 = new Book2('Book One','John','2012');
const book22 = new Book2('Book Two','Tom','2016');

console.log('book21.getSummary():',book21.getSummary());
console.log('book22.getSummary():',book22.getSummary());
console.log('book21:',book21);
console.log('book22:',book22);

3. 原型与原型链

/*
1. 所有的实例都会通过原型链引用到prototype
2. prototype相当于特定类型,所有实例都可以访问到一个公共容器
3. 重复的东西放到公共的容器
*/
function Book(title,author,year)
{
    this.title = title;
    this.author = author;
    this.year = year;
}
// getSummary获取摘要
Book.prototype.getSummary =function ()
{
    return `${this.title} was written by ${this.author} in ${this.year}`
}
// getAge 发行距今多长时间
Book.prototype.getAge = function ()
{
    const years = new Date().getFullYear -this.year;
    return `${this.title} is ${years} years old`;
}
// Revise修订
Book.prototype.revise = function (newYear)
{
    this.year = newYear;
    this.revised = true;
}

const book1 = new Book('Book One','John','2012');
const book2 = new Book('Book Two','Tom','2016');

console.log('book1:',book1);
console.log('book2:',book2);
console.log('book1.getSummary():',book1.getSummary());
console.log('book2.getAge():',book2.getAge());

// getSummary存在于原型中,而不存在对象本身
// 生成的实例不一定需要getSummary,而title属性就需要每个都有

const book3 = new Book('Book Two','Tom','2019');
book3.revise('2022')
console.log('book3:',book3);

4. 继承

// 继承
function Book(title,author,year)
{
    this.title = title;
    this.author = author;
    this.year = year;
};
// getSummary获取摘要
Book.prototype.getSummary =function ()
{
    return `${this.title} was written by ${this.author} in ${this.year}`
}
// 杂志构造函数
function Magazine(title,author,year,month)
{
    Book.call(this,title,author,year);
    this.month = month;
}
// inherit prototype 继承原型
Magazine.prototype = Object.create(Book.prototype);

//  实例化杂志对象
const mag1 = new Magazine('Mag One','Tom','2022','Jan')

// 使用Magazine构造器
Magazine.prototype.constructor = Magazine;

console.log('mag1:',mag1);
console.log('mag1.getSummary():',mag1.getSummary());
  • 不使用Magazine构造器

  • 使用Magazine构造器

5. 对象的创建

// 创建对象的多种方法
//  object of protos
const bookProtos =
{
    getSummary: function()
    {
        return `${this.title} was written by ${this.author} in ${this.year}`
    },
    getAge:function()
    {
        const years = new Date().getFullYear -this.year;
        return `${this.title} is ${years} years old`;
    }
};

// Create Object
// 方法1(推荐)
const book1 = Object.create(bookProtos);
book1.title='Book One';
book1.author='Tom';
book1.year='2012';

console.log('book1:',book1);

// 方法二
const book2 = Object.create
(bookProtos,
    {
        title   :{value : 'Book One'},
        author  :{value : 'Tom'},
        year    :{value : '2012'}
    }

);

console.log('book2:',book2);

6. 类

// 类
class Book
{
    // constructor构造器
    constructor(title,author,year)
    {
        this.title = title;
        this.author = author;
        this.year = year;
    }
    getSummary()
    {
        return `${this.title} was written by ${this.author} in ${this.year}`
    }
    getAge()
    {
        const years = new Date().getFullYear -this.year;
        return `${this.title} is ${years} years old`;
    }
    revise(newYear)
    {
        this.year = newYear;
        this.revised = true;
    }
    static topBookStore()
    {
        return 'Barnes & Noble';
    }
}

// Instantiate Object 实例化对象
const book1 = new Book('Book One','Tom','2012')

console.log(book1);
book1.revise('2020');
console.log(book1);

// static 方法
console.log(Book.topBookStore());

7. 子类

// 子类
class Book
{
    constructor(title,author,year)
    {
        this.title=title;
        this.author=author;
        this.year=year;
    }
    getSummary()
    {
        return `${this.title} was written by ${this.author} in ${this.year}`
    }
}

// Magazine Subclass
class Magazine extends Book
{
    constructor(title,author,year,month)
    {
        super(title,author,year)
        this.month=month;
    }
}

// Instantiate Magazine
const mag1 = new Magazine('Mag One','Tom','2022','Jan');
console.log('mag1:',mag1);
console.log(mag1.getSummary());

标签:06,log,author,JavaScript,console,面向对象,Book,year,title
From: https://www.cnblogs.com/leoshi/p/16876645.html

相关文章

  • simpread-获取 JavaScript 对象的键 _ D 栈 - Delft Stack
    本文由简悦SimpRead转码,原文地址www.delftstack.com使用Object.keys()方法获取javascript对象的键Object.keys()函数返回一个包含javascript对象键的数组......
  • 中文书籍对《人月神话》的引用(20211105更新161-165本):大师品软件、JavaScript开发框架
    ​​中文书籍对《人月神话》的引用(第001到160本)>>​​《人月神话》于1975年出版,1995年出二十周年版。自出版以来,该书被大量的书籍和文章引用,直到现在热潮不退。UMLChina摘录......
  • H5游戏开发-面向对象编程
    七、面向对象编程1.认识类与对象类是一种复杂的数据结构,他是将不同类型的数据与这些数据相关的操作封装在一起的集合体。对象是类的实例,是类描述的具体事物。(类是对象的......
  • Team Weekly Contest 2022-11-06
    2022ICPCAsiaTaiwanOnlineProgrammingContestH.Heximal不会高精度,拿python写的,但是python3.8会TLE,python2不会,就是有点卡高精度+快速幂deffp(x,y):ret=......
  • JavaScript错误1
    JavaScript:Failedtoloadresource:theserverrespondedwithastatusof404(NotFound)  解决方法:1.打开idea设置,找到JavaScript保存即可  2.点击ok ......
  • [JavaScript-05]函数和箭头函数
    1.函数functionaddNums(num1=1,num2=2){console.log('num1+num2:',num1+num2);returnnum1+num2;}addNums();addNums(5,6);console.log('addNums(......
  • Day06:运算符详解
    运算符算术运算符:+,-,*,/,%(取余;也叫模运算),++(自增),--(自减)...........二次运算符+,-,*,/inta=10;intb=20;intc=50;intd=100;System.out.println(a+b);//=30System.o......
  • [JavaScript-03]IF 三元表达式 逻辑运算 == ===
    1.语句//if语句letx=10;if(x==10){console.log('xis10')};//ifelseifelsex=20;if(x===10){console.log('xis10');}elsei......
  • [JavaScript-04]Switch
    1.Switch//Switch语句constcolor='green';switch(color){case'red':console.log('colorisred');break;case'blue':......
  • [JavaScript-02]FOR WHILE循环
    1.语句//For语句for(letindex=0;index<=6;index++){console.log(`ForLoopNumber:${index}`);}//While语句leti=0;while(i<=6){c......