How to Create a JavaScript Wrapper for API
An API wrapper, short for Application Programming Interface wrapper, in its most basic form is a programming statement that calls another group of programming statements. JavaScript has several methods that can be called to create a JavaScript API. Not only that, with the JavaScript "function" command you can create your own custom API methods. These API methods can then be called within your JavaScript program with the JavaScript function calling syntax (the wrapper syntax). This wrapper syntax will follow the same syntax as the function you created.
Instructions
-
-
1
Create a text file with your text editor. Save the file as "wrapper.html."
-
2
Type in the code on the first line of the text editor the "HTML" tag to indicate to the Web browser that the page is an HTML-formatted document. Include the JavaScript script tag to indicate to the browser that it should interpret the instructions that follow as JavaScript code:
<html>
<head>
<script type="text/javascript">
-
-
3
Type, starting at the next line in the text editor, the code to create a custom JavaScript function that divides two numbers that are stored in a variable named Y1 and a variable named X1 and returns the result. Name the function "division" (within the function statement):
function division(Y1,X1)
{
return X1/X1;
}
-
4
Type, starting at the next line in the text editor, the code to close the JavaScript "script" tag and the HTML "head" tag:
</script>
</head>
-
5
Type, starting at the next line in the text editor, the tag that starts the body of the HTML document:
<body>
-
6
Type, starting at the next line in the text editor, the code to create the JavaScript Wrapper API for the function you called "division." Specify the arguments for the wrapper to be the values of Y1 and X1 that you want to divide. Assign the wrapper as the argument for the JavaScript "write" method and attach it to the "document" element so that the result of the wrapper call to the "division" function will display the result of the division on the Web page displayed.
document.write(division(10, 2));
-
7
Type, starting at the next line in the text editor, the code to close the "body" tag and the "html" tag:
</body>
</html>
-
1
References
- Photo Credit Hemera Technologies/PhotoObjects.net/Getty Images