一个ABAP和JavaScript这两种编程语言的横向比较-阿里云开发者社区

开发者社区> jerrywangsap> 正文

一个ABAP和JavaScript这两种编程语言的横向比较

简介: 一个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.


image.png

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.


image.png


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”,

image.png

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.


image.png


Finally it generates output like below:


I am function a

I am function b

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:image.png

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.


image.png


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

image.pngexecute example 2 again, the output is generated with our customized color:


image.png


(4) Constructor redefinition

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


image.png


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:

image.pngimage.pngimage.pngimage.pngimage.pngimage.pngimage.pngimage.pngimage.pngimage.pngimage.pngimage.pngimage.pngFurther 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.


image.png


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

image.pngimage.png

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.


image.png


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

image.png

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.


image.png


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



image.png

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


image.png


(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:


image.png


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


image.png


still it will end up with runtime error:



image.png

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


image.png


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();

image.png

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.


image.png


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:

image.png

var first_name = "Thomas";

var family_name = "Zhang";

var func = person.introduce;

func();


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


image.png


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

image.png


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.


版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。

相关文章
使用NAT网关轻松为单台云服务器设置多个公网IP
在应用中,有时会遇到用户询问如何使单台云服务器具备多个公网IP的问题。 具体如何操作呢,有了NAT网关这个也不是难题。
26797 0
阿里云服务器怎么设置密码?怎么停机?怎么重启服务器?
如果在创建实例时没有设置密码,或者密码丢失,您可以在控制台上重新设置实例的登录密码。本文仅描述如何在 ECS 管理控制台上修改实例登录密码。
10095 0
阿里云服务器端口号设置
阿里云服务器初级使用者可能面临的问题之一. 使用tomcat或者其他服务器软件设置端口号后,比如 一些不是默认的, mysql的 3306, mssql的1433,有时候打不开网页, 原因是没有在ecs安全组去设置这个端口号. 解决: 点击ecs下网络和安全下的安全组 在弹出的安全组中,如果没有就新建安全组,然后点击配置规则 最后如上图点击添加...或快速创建.   have fun!  将编程看作是一门艺术,而不单单是个技术。
10886 0
阿里云服务器如何登录?阿里云服务器的三种登录方法
购买阿里云ECS云服务器后如何登录?场景不同,阿里云优惠总结大概有三种登录方式: 登录到ECS云服务器控制台 在ECS云服务器控制台用户可以更改密码、更换系.
13893 0
windows server 2008阿里云ECS服务器安全设置
最近我们Sinesafe安全公司在为客户使用阿里云ecs服务器做安全的过程中,发现服务器基础安全性都没有做。为了为站长们提供更加有效的安全基础解决方案,我们Sinesafe将对阿里云服务器win2008 系统进行基础安全部署实战过程! 比较重要的几部分 1.
9161 0
阿里云服务器安全组设置内网互通的方法
虽然0.0.0.0/0使用非常方便,但是发现很多同学使用它来做内网互通,这是有安全风险的,实例有可能会在经典网络被内网IP访问到。下面介绍一下四种安全的内网互联设置方法。 购买前请先:领取阿里云幸运券,有很多优惠,可到下文中领取。
11819 0
腾讯云服务器 设置ngxin + fastdfs +tomcat 开机自启动
在tomcat中新建一个可以启动的 .sh 脚本文件 /usr/local/tomcat7/bin/ export JAVA_HOME=/usr/local/java/jdk7 export PATH=$JAVA_HOME/bin/:$PATH export CLASSPATH=.
4664 0
阿里云ECS云服务器初始化设置教程方法
阿里云ECS云服务器初始化是指将云服务器系统恢复到最初状态的过程,阿里云的服务器初始化是通过更换系统盘来实现的,是免费的,阿里云百科网分享服务器初始化教程: 服务器初始化教程方法 本文的服务器初始化是指将ECS云服务器系统恢复到最初状态,服务器中的数据也会被清空,所以初始化之前一定要先备份好。
7365 0
阿里云服务器如何登录?阿里云服务器的三种登录方法
购买阿里云ECS云服务器后如何登录?场景不同,云吞铺子总结大概有三种登录方式: 登录到ECS云服务器控制台 在ECS云服务器控制台用户可以更改密码、更换系统盘、创建快照、配置安全组等操作如何登录ECS云服务器控制台? 1、先登录到阿里云ECS服务器控制台 2、点击顶部的“控制台” 3、通过左侧栏,切换到“云服务器ECS”即可,如下图所示 通过ECS控制台的远程连接来登录到云服务器 阿里云ECS云服务器自带远程连接功能,使用该功能可以登录到云服务器,简单且方便,如下图:点击“远程连接”,第一次连接会自动生成6位数字密码,输入密码即可登录到云服务器上。
22412 0
+关注
2628
文章
0
问答
来源圈子
更多
+ 订阅
文章排行榜
最热
最新
相关电子书
更多
《2021云上架构与运维峰会演讲合集》
立即下载
《零基础CSS入门教程》
立即下载
《零基础HTML入门教程》
立即下载