SAP Fiori开发中的JavaScript基础知识19 - 综合练习

1. 背景

本篇博客将通过一个例子,来融汇贯通JavaScript中的基础知识点。

2. 题目说明

创建一个小型车队管理器应用程序。它维护两种类型的车辆:汽车和摩托车。

  • Vehicle定义了两个通用属性:车辆名称和制造年份。它包含一个print函数,用于打印车辆数据。

  • Car扩展了Vehicle,它包含另外两个属性:人数和发动机大小。

  • Motorbike扩展了Vehicle,它包含一个额外的属性来存储最大速度。

创建一个通用的打印机函数表达式,用于将汽车或摩托车数据打印到UI。

  • 使用不同类型的函数调用来显示数据(显式或隐式绑定)。

3. 代码实现

3.1 利用原型模式

javascript 复制代码
var printService = function () {
    // this is used to access the bound object
    var sOutput = this.getName() + " (" + this.getYear + ")";

    if (this.engine) { sOutput += " Engine:" + this.getEngine() };
    if (this.persons) { sOutput += " Persons:" + this.getPersons() };
    if (this.speed) { sOutput += " Max speed:" + this.getMaxSpeed() };

    console.log(sOutput);
}

function Vehicle(sName, iYear) {
    this.name = sName;
    this.year = iYear;
}

//.prototype assignment makes these properties available for children
Vehicle.prototype.name = "";
Vehicle.prototype.year = "";
Vehicle.prototype.getName = function () { return this.name };
Vehicle.prototype.getYear = function () { return this.year };
Vehicle.prototype.print = printService; // use existing function expression ('this' is bound automatically)


function Car(sName, iYear, iEngine, iPersons) {
    Vehicle.call(this, sName, iYear); // call the constructor of the parent
    this.engine = iEngine;
    this.persons = iPersons;
}

//.prototype assignment makes these properties available for children
Car.prototype = Object.create(Vehicle.prototype);
Car.prototype.engine = "";
Car.prototype.persons = "";
Car.prototype.getEngine = function () { return this.engine };
Car.prototype.getPersons = function () { return this.persons };


function Motorcycle(sName, iYear, iMaxSpeed) {
    Vehicle.call(this, sName, iYear); //call the constructor of the parent
    this.speed = iMaxSpeed;
}

//.prototype assignment makes these properties available for children
Motorcycle.prototype = Object.create(Vehicle.prototype);
Motorcycle.prototype.speed = "";
Motorcycle.prototype.getMaxSpeed = function () { return this.speed };

// create one-one instance for Car and Mortocycle
var myCar = new Car("Opel Astra", 2014, 1389, 5);
var myMotorcycle = new Motorcycle("Honda", 2008, 216);

//log fleet data
printService.call(myCar); // explicit binding
printService.call(myMotorcycle); // explicit binding

myCar.print(); //implicit binding
myMotorcycle.print(); //implicit binding

var fnCarPrintService = printService.bind(myCar); // explicit binding; new function is required
fnCarPrintService();
var fnMotorcyclePrintService = printService.bind(myMotorcycle);
fnMotorcyclePrintService();

//printService(); it does not work because 'this' is set to global in non-strict mode and default binding

最终实现以下效果:

基于Car生成的myCar实例 -

基于Motrocycle生成的myMotrocycle实例:

3.2 利用class语法糖改写

javascript 复制代码
var printService = function () {
    // this is used to access the bound object
    var sOutput = this.getName() + " (" + this.getYear + ")";

    if (this.engine) { sOutput += " Engine:" + this.getEngine() };
    if (this.persons) { sOutput += " Persons:" + this.getPersons() };
    if (this.speed) { sOutput += " Max speed:" + this.getMaxSpeed() };

    console.log(sOutput);
}

class Vehicle {
    constructor(sName, iYear) {
        this.name = sName;
        this.year = iYear;
    }
    getName = function () { return this.name };
    getYear = function () { return this.year };
    print = printService; // use existing function expression ('this' is bound automatically)
}

class Car extends Vehicle {
    constructor(sName, iYear, iEngine, iPersons) {
        super(sName, iYear); // call the constructor of the parent
        this.engine = iEngine;
        this.persons = iPersons;
    }
    getEngine = function () { return this.engine };
    getPersons = function () { return this.persons };
}

class Motorcycle extends Vehicle {
    constructor(sName, iYear, iMaxSpeed) {
        super(sName, iYear); //call the constructor of the parent
        this.speed = iMaxSpeed;
    }
    getMaxSpeed = function () { return this.speed };
}

// create one-one instance for Car and Mortocycle
var myCar = new Car("Opel Astra", 2014, 1389, 5);
var myMotorcycle = new Motorcycle("Honda", 2008, 216);

//log fleet data
printService.call(myCar); // explicit binding
printService.call(myMotorcycle); // explicit binding

myCar.print(); //implicit binding
myMotorcycle.print(); //implicit binding

var fnCarPrintService = printService.bind(myCar); // explicit binding; new function is required
fnCarPrintService();
var fnMotorcyclePrintService = printService.bind(myMotorcycle);
fnMotorcyclePrintService();

//printService(); it does not work because 'this' is set to global in non-strict mode and default binding

最终实现以下效果:

基于Car生成的myCar实例 -

基于Motrocycle生成的myMotrocycle实例:

4. 小结

本篇博客将通过一个例子,融汇贯通了JavaScript中的基础知识点。在运行代码时,可以通过调试模式来查看代码的运行逻辑。

希望本文对你有帮助!

相关推荐
熊的猫44 分钟前
JS 中的类型 & 类型判断 & 类型转换
前端·javascript·vue.js·chrome·react.js·前端框架·node.js
别拿曾经看以后~2 小时前
【el-form】记一例好用的el-input输入框回车调接口和el-button按钮防重点击
javascript·vue.js·elementui
川石课堂软件测试2 小时前
性能测试|docker容器下搭建JMeter+Grafana+Influxdb监控可视化平台
运维·javascript·深度学习·jmeter·docker·容器·grafana
JerryXZR3 小时前
前端开发中ES6的技术细节二
前端·javascript·es6
problc3 小时前
Flutter中文字体设置指南:打造个性化的应用体验
android·javascript·flutter
Gavin_9153 小时前
【JavaScript】模块化开发
前端·javascript·vue.js
懒大王爱吃狼4 小时前
Python教程:python枚举类定义和使用
开发语言·前端·javascript·python·python基础·python编程·python书籍
待磨的钝刨5 小时前
【格式化查看JSON文件】coco的json文件内容都在一行如何按照json格式查看
开发语言·javascript·json
前端青山10 小时前
Node.js-增强 API 安全性和性能优化
开发语言·前端·javascript·性能优化·前端框架·node.js
从兄11 小时前
vue 使用docx-preview 预览替换文档内的特定变量
javascript·vue.js·ecmascript