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

1. 背景

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

2. 题目说明

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

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

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

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

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

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

3. 代码实现

3.1 利用原型模式

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语法糖改写

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中的基础知识点。在运行代码时,可以通过调试模式来查看代码的运行逻辑。

希望本文对你有帮助!

  • 10
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
SAP Fiori是一种用于开发现代化、易于使用和美观的SAP应用程序的User Experience(UX)设计原则和技术。以下是SAP Fiori开发的步骤: 1. 理解业务需求:在开始SAP Fiori开发之前,首先需要与业务团队合作,深入了解他们的需求和期望。这将帮助你确定开发的目标和范围。 2. 设计用户体验:基于业务需求,设计用户界面和用户体验。这包括使用SAP Fiori的设计原则创建简洁、直观和易于导航的应用程序。 3. 创建SAP Fiori应用程序:使用SAP Web IDE或ABAP开发工具创建SAP Fiori应用程序。这些工具提供了创建和定制应用程序所需的开发环境和工具。 4. 集成业务逻辑:在应用程序集成业务逻辑和SAP后端系统。这涉及使用SAP Gateway或OData服务与后端系统进行集成,并确保应用程序能够正确处理和显示数据。 5. 进行测试:在部署应用程序之前,进行必要的测试和调试。这包括功能测试、用户界面测试和性能测试,以确保应用程序在不同情况下能够正常运行。 6. 部署应用程序:在SAP系统部署SAP Fiori应用程序。这可以通过将应用程序打包为SAP Fiori Launchpad以及使用SAP Fiori Launchpad Designer来配置启动页面和导航。 7. 进行维护和优化:一旦应用程序部署,需要进行持续的维护和优化。这包括修复错误、添加新功能和优化性能,以确保应用程序始终提供最佳的用户体验。 总结来说,SAP Fiori开发步骤包括理解业务需求、设计用户体验、创建应用程序、集成业务逻辑、测试、部署和维护。这些步骤将帮助开发人员通过遵循SAP Fiori的设计原则和使用相关开发工具来创建现代化的SAP应用程序。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

SAP-nkGavin

给作者赏杯咖啡

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值