天天看點

一個ABAP和JavaScript這兩種程式設計語言的橫向比較

There are already two great blogs which stress why ABAPers should learn Javascript and some essential Javascript features.

Top 10 things ABAP developers should know when learning JavaScript

JavaScript for ABAP Developers

In this document, I will collect some interesting and useful Javascript features compared with ABAP by using examples for demonstration. I will keep it updated once I have learned new stuff.

(1) An object could consume a function which it does not own

Paste this source code below into a new .html file and run it in Chrome.

一個ABAP和JavaScript這兩種程式設計語言的橫向比較

All the codes are very easy to understand except this “iTom.codeC.call(iJerry)”.

As mentioned in Kevin Small’s blog, “In JavaScript Functions are First Class”, we can consider currently Javascript uses the keyword “function” to implement? the OO concept with different approach than ABAP. In this example, although Jerry has no C development experience, now I suddenly owned 7 years’ C development experience on Linux

Output in Chrome console:

Hello, my name is: Jerry I have 7 year’s ABAP development experience

Hello, my name is: Tom I have 20 year’s C development experience focus on Linux system.

Hello, my name is: Jerry I have 7 year’s C development experience focus on Linux system.

If you debug in Chrome, you can find the magic of iTom.codeC.call(iJerry). Here the function codeC owned by Object CDeveloper is called by explicitly specifying the context as instance iJerry, this could be observed by checking “this” variable in Chrome debugger, currently “this” points to instance iJerry.

一個ABAP和JavaScript這兩種程式設計語言的橫向比較

This quite flexible feature is not available in ABAP. As we know, the class instance in ABAP could only consume its own method or those inherited from its parent.

(2) Anonymous object

in below example, we define two simple functions a and b, and assign them separately to variable d and e, so those two functions could also be called via d and e as well. In the meantime, another anonymous function with one argument name is also defined. in this context, there is no way to assign this anonymous function to another variable, it has to be executed immediately once having been defined. In this example it is called with argument name = “c”,

一個ABAP和JavaScript這兩種程式設計語言的橫向比較

Check the variable a , b, d, e in Chrome debugger. There is a tab “anonymous function” which enables you to have a list of all anonymous functions used.

一個ABAP和JavaScript這兩種程式設計語言的橫向比較

Finally it generates output like below:

I am function a

I am function b

I am function: c

Go back to ABAP, since we don’t treat function module as an object, and every function module should have a name so that it could be called via name.

And for the anonymous function in Javascript, since once it is defined and executed, it could not be reused later, I personaly would like to compare this “transient” feature with ABAP keyword GENERATE SUBROUTINE POOL itab NAME prog. Just execute this code which could be found in ABAP help:

一個ABAP和JavaScript這兩種程式設計語言的橫向比較

In the runtime, a temporary class type is created and it is allowed to create new object instance based on this type. And this type is transient so could only be used in current transaction.

(3) Overwrite builder-in code

Let’s review how could this be done in ABAP. It is one of the most powerful abilities I appreciate in ABAP to change the behavior of standard code via pre-exit, post-exit and overwrite-exit. It is a good tool especially for those consultant working for customer project. By creating an overwrite-exit, you can completely deactivate the standard method execution and make your own exit run. For details about how to create the exit, please see this document.

一個ABAP和JavaScript這兩種程式設計語言的橫向比較

And in Javascript, it is even much easier to overwrite the build-in code. Suppose I would like to rewrite the build-in function console.log.

Just paste this code below in the beginning of

一個ABAP和JavaScript這兩種程式設計語言的橫向比較

execute example 2 again, the output is generated with our customized color:

一個ABAP和JavaScript這兩種程式設計語言的橫向比較

(4) Constructor redefinition

In ABAP it is not possible to redefine a constructor method.

一個ABAP和JavaScript這兩種程式設計語言的橫向比較

You will receive a message when clicking redefine button in class builder: “You cannot redefine the constructor”.

However in Javascript, it is allowed to redefine a function implementation inside itself, sounds crazy?

Let’s first have a look at this small piece of code:

一個ABAP和JavaScript這兩種程式設計語言的橫向比較
一個ABAP和JavaScript這兩種程式設計語言的橫向比較
一個ABAP和JavaScript這兩種程式設計語言的橫向比較
一個ABAP和JavaScript這兩種程式設計語言的橫向比較
一個ABAP和JavaScript這兩種程式設計語言的橫向比較
一個ABAP和JavaScript這兩種程式設計語言的橫向比較
一個ABAP和JavaScript這兩種程式設計語言的橫向比較
一個ABAP和JavaScript這兩種程式設計語言的橫向比較
一個ABAP和JavaScript這兩種程式設計語言的橫向比較
一個ABAP和JavaScript這兩種程式設計語言的橫向比較
一個ABAP和JavaScript這兩種程式設計語言的橫向比較
一個ABAP和JavaScript這兩種程式設計語言的橫向比較
一個ABAP和JavaScript這兩種程式設計語言的橫向比較

Further discussion on ABAP function module passing approach

For me, during my project which involves SAP system integration with third party application, it is required that the signature of ABAP function module must be kept stable but flexible and open for further enhancement. In such case I will use the “name-value pairs” to hold the importing parameter.

Now the signature has been changed to a table type whose line type is a structure with two fields: NAME and VALUE.

一個ABAP和JavaScript這兩種程式設計語言的橫向比較

Now inside the function module I have to evaluate each parameter to check whether it is passed in by myself:

一個ABAP和JavaScript這兩種程式設計語言的橫向比較
一個ABAP和JavaScript這兩種程式設計語言的橫向比較

By using this name-value pair, next time if the third party application asks the SAP application to support some new parameter, there is no need to change the current signature – new supported parameter could still be passed in as name value pair, and just a new WHEN branch needs to be added in function module body.

(8) predefined data type and wrapper object if any

There are lots of predefined types in ABAP which you could find a complete list from ABAP help.

一個ABAP和JavaScript這兩種程式設計語言的橫向比較

In ABAP, you could not create an object instance based on a predefined ABAP type via key word CREATE OBJECT as below:

一個ABAP和JavaScript這兩種程式設計語言的橫向比較

DATA: lv_integer TYPE REF TO int4.

CREATE OBJECT lv_integer.

You will receive syntax error “LV_INTEGER” is not an object reference. It is only possible to create data reference via CREATE DATA.

In Javascript, most predefined data type ( except null and undefined ) like Number, String and Boolean have their corresponding wrapper object. Open your Chrome, launch development tool via F12 and do the test below.

Type 1.toString() in console and you will get syntax error, since 1 is not an object instance, so it is wrong to try to perform the object method toString.

一個ABAP和JavaScript這兩種程式設計語言的橫向比較

if you change to (1), then the method execution works, because by using “()”, an instance of wrapper object of type Number is created implicitly.

一個ABAP和JavaScript這兩種程式設計語言的橫向比較

Actually (1).toString() equals to the last two lines below:

一個ABAP和JavaScript這兩種程式設計語言的橫向比較

(9) me in ABAP VS this in Javascript

Let me reuse the definition on “me” in ABAP help: within the implementation of every instance method, an implicitly created local reference variable called me is available, which points to the instance in which the method is currently being executed. me is treated like a local constant, that means the value of me cannot be altered in an instance method.

The example below shows the me reference is protected inside the instance method:

一個ABAP和JavaScript這兩種程式設計語言的橫向比較

although you could use the code below to pass the syntax check,

一個ABAP和JavaScript這兩種程式設計語言的橫向比較

still it will end up with runtime error:

一個ABAP和JavaScript這兩種程式設計語言的橫向比較

The example below shows “me” is reserved word in ABAP and could not be used to name attribute, parameter and variable.

一個ABAP和JavaScript這兩種程式設計語言的橫向比較

If you have ever programmed using C++, C# and Java, the usage of “this” is not new to you. However since Javascriot does not have built-in support for class concept, there is still some difference of “this” usage. Consider the following code:

function Person(first_name, family_name) {

 this.first_name = first_name;

 this.family_name = family_name;

 this.introduce = function() {

 console.log("Hello everyone, I am " + this.first_name + " , " + this.family_name );

 }

}

var person = new Person("Jerry", "Wang");

person.introduce();

var func = person.introduce;

func();

一個ABAP和JavaScript這兩種程式設計語言的橫向比較

Firstly we call instance method “introduce” via the object reference person, and works as expected. Secondly I pass the “introduce” method of person reference to another variable func. This time both name are printed as “undefined”.

Hello everyone, I am Jerry, Wang

Hello everyone, I am undefined, undefined

We could find root cause in debugger. When “introduce” method is called through variable func, in the runtime “this” does not hold the reference of variable person, but points to global object Window instead. Since both attributes “first_name” and “family_name” do not exist in Window, we get “undefined” printed.

一個ABAP和JavaScript這兩種程式設計語言的橫向比較

So the rule is, when a method ( to be more exactly, a function ) is called via the approach <object_reference>., inside the method execution, “this” will be set as <object_reference> by runtime. In other cases, “this” will point to the global variable Window.

It is known that any variable defined outside functions will become as an attribute in global variable Window.

We could verify this rule by adding two lines below:

一個ABAP和JavaScript這兩種程式設計語言的橫向比較

var first_name = "Thomas";

var family_name = "Zhang";

And we can see two new attributes in Window variable in debugger:

一個ABAP和JavaScript這兩種程式設計語言的橫向比較

And the introduce method actually prints the name from Window this time.

一個ABAP和JavaScript這兩種程式設計語言的橫向比較

Using bind method, we could really specify an object reference which will be treated as “this” when the function who call “bind” is executed in the runtime.