Skip to content

Example of a Class

Marco Beier edited this page Jun 22, 2020 · 4 revisions

The example shows a class named "Person". It extends the UI5 base object "sap.ui.base.Object".

It consists of:

  • constructor
  • attributes (model; first-, last- and fullname)
  • methods (functions: getFullName and getModel)
sap.ui.define([
  "sap/ui/base/Object",
  "sap/ui/model/json/JSONModel"
], function(Object, JSONModel) {
  "use strict";
    return Object.extend("some.Namespace.Folder.Person", {
        constructor: function(data) {
            if (data) {
                this.firstname = data.firstname;
                this.lastname  = data.lastname;
                this.fullname  = this.getFullName();
            }
            this.model = new JSONModel();
            this.model.setData(this);
        },
        getFullName: function() {
            return this.firstname + " " + this.lastname;
        },
        getModel: function() {
            return this.model;
        }
    });
});

Reminder what the .extend method actually takes in as arguments:

  • sClassName - string - name of the class to be created
  • oClassInfo? - object - structured object with information about the class
  • FNMetaImpl? - function - constructor function for the metadata object. If not given, it defaults to sap.ui.base.Metadata.

Clone this wiki locally