Escolar Documentos
Profissional Documentos
Cultura Documentos
2.
3.
4.
5.
6.
7.
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.
<html>
2.
<script>
3.
4.
5.
this.name = name;
6.
this.year = year;
7.
this.codeABAP = function() {
8.
9.
10. }
11. }
12. function CDeveloper(name, year, os)
13. {
14. this.name = name;
15. this.year = year;
16. this.os = os;
17. this.codeC = function() {
18. console.log("Hello, my name is: " + this.name + " I have " + this.year
19. + " year's C development experience focus on " + os + " system.");
20. }
21. }
22. var iJerry = new ABAPDeveloper("Jerry", 7);
23. iJerry.codeABAP();
24. var iTom = new CDeveloper("Tom", 20, "Linux");
25. iTom.codeC();
26. iTom.codeC.call(iJerry);
27. </script>
28. </html>
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",
1.
<html>
2.
<p>hello</p>
3.
<script>
4.
5.
6.
var d = a;
7.
var e = b;
8.
a();
9.
b();
10. d();
11. e();
12. (function(name) { console.log("I am function: " + name); })("c");
13. </script>
14. </html>
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:
1.
2.
3.
4.
5.
APPEND `program.`
6.
7.
8.
APPEND `
9.
APPEND `endclass.`
methods meth.`
TO itab.
TO itab.
TO itab.
TO itab.
TO itab.
TO itab.
TO itab.
TO itab.
1.
2.
console.log = function() {
3.
4.
};
execute example 2 again, the output is generated with our customized color:
4. Constructor redefinition
In ABAP it is not possible to redefine a constructor method.
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:
1.
<html
2.
</html>
3.
<script>
4.
function Person(name)
5.
{
this.name = name;
6.
7.
8.
9.
it pops up No, since we use "===" comparator and person1 and person2 are
two different object instance.
Now inside function Person, I overwrite it by just return the buffered "this"
instance, so all the subsequent call of new Person will always return that
buffered instance, and this time I will save a pop up with Yes.
<html>
<script>
function Person(name) {
var instance = this;
this.name = name;
Person = function() {
return instance;
}
}
var iJerry1 = new Person("Jerry");
var iJerry2 = new Person("Jerry");
alert( iJerry1 === iJerry2 ? "Yes":"No");
</script>
</html>
1.
a(), b();
1.
this.name = name;
this.language = language;
this.year = year;
}
var iJerry = new Developer("Jerry", "ABAP", "7");
var iTom = new Developer("Tom", "Java");
var iBob = new Developer("Bob");
</script>
</html>
Execute the code using Chrome debugger. Then we can observe from
debugger that if a given parameter is not specified during function call, the
corresponding field inside the Developer will hold an uninitialized value "undefined."
FUNCTION ZABAP_FM_ARGUMENT.
WRITE: / 'Para1:', iv_para1.
WRITE: / 'Para2:', iv_para2.
WRITE: / 'Para3:', iv_para3.
WRITE: / 'Para4:', iv_para4.
IF iv_para1 IS SUPPLIED.
REPORT zpara_test.
PARAMETERS: p1 AS CHECKBOX,
p2 AS CHECKBOX,
p3 AS CHECKBOX,
p4 AS CHECKBOX.
DATA: lt_ptab TYPE abap_func_parmbind_tab,
ls_ptab1 LIKE LINE OF lt_ptab,
ls_ptab2 LIKE LINE OF lt_ptab,
ls_ptab3 LIKE LINE OF lt_ptab,
ls_ptab4 LIKE LINE OF lt_ptab,
lv_para1 TYPE int4 VALUE 1,
lv_para2 TYPE int4 VALUE 2,
lv_para3 TYPE int4 VALUE 3,
lv_para4 TYPE int4 VALUE 4,
start-of-selection.
IF p1 = 'X'.
ls_ptab1-name = 'IV_PARA1'.
ls_ptab1-kind = abap_func_exporting.
GET REFERENCE OF lv_para1 INTO ls_ptab1-value.
APPEND ls_ptab1 TO lt_ptab.
ENDIF.
IF p2 = 'X'.
ls_ptab2-name = 'IV_PARA2'.
ls_ptab2-kind = abap_func_exporting.
Now inside the function module I have to evaluate each parameter to check
whether it is passed in by myself:
FUNCTION ZABAP_FM_ARGUMENT1.
DATA: ls_para like line of it_para.
LOOP AT it_para INTO ls_para.
CASE ls_para-name.
WHEN 'IV_PARA1'.
WRITE: / 'Para 1 is
WHEN 'IV_PARA2'.
WRITE: / 'Para 2 is
WHEN 'IV_PARA3'.
WRITE: / 'Para 3 is
WHEN 'IV_PARA4'.
WRITE: / 'Para 4 is
ENDCASE.
ENDLOOP.
ENDFUNCTION.
REPORT zpara_test.
PARAMETERS: p1 AS CHECKBOX,
p2 AS CHECKBOX,
p3 AS CHECKBOX,
p4 AS CHECKBOX.
DATA: ls_nv_pair TYPE CRM_IC_NAME_VALUE,
lt_nv_pair TYPE CRM_IC_NAME_VALUE_TA.
start-of-selection.
IF p1 = 'X'.
ls_nv_pair-name = 'IV_PARA1'.
ls_nv_pair-value = 1.
APPEND ls_nv_pair TO lt_nv_pair.
ENDIF.
IF p2 = 'X'.
ls_nv_pair-name = 'IV_PARA2'.
ls_nv_pair-value = 2.
APPEND ls_nv_pair TO lt_nv_pair.
ENDIF.
IF p3 = 'X'.
ls_nv_pair-name = 'IV_PARA3'.
ls_nv_pair-value = 3.
APPEND ls_nv_pair TO lt_nv_pair.
ENDIF.
IF p4 = 'X'.
ls_nv_pair-name = 'IV_PARA4'.
ls_nv_pair-value = 4.
APPEND ls_nv_pair TO lt_nv_pair.
ENDIF.
CALL FUNCTION 'ZABAP_FM_ARGUMENT1'
EXPORTING
it_para = lt_nv_pair.
By using this name-value pair, next time if the thrid 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.
In ABAP, you could not create an object instance based on a predefined ABAP
type via key word CREATE OBJECT as below:
DATA: lv_integer TYPE REF TO int4.
CREATE OBJECT lv_integer.
if you change to (1), then the method execution works, because by using
"()", an instance of wrapper object of type Number is created implicitly.
although you could use the code below to pass the syntax check,
The example below shows "me" is reserved word in ABAP and could not be
used to name attribute, parameter and variable.
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:
<script>
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();
</script>
Firstly we call instance method "introduce" via the object reference person,
and works as expectedly. 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.
So the rule is, when a method ( to be more exactly, a function ) is called via
the approach <object_reference>.<method>, 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:
And the introduce method actually prints the name from Window this time.
Hello everyone, I am Jerry, Wang
Hello everyone, I am Thomas, Zhang
There is another way which could achieve the same result to get Thomas,
Zhang printed:
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.