crazy novice
crazy novice

Reputation: 1817

class library project using javascript/hmlt5. Is it possible?

I am learning to create Windows 8 store application using java-script and html5. My question is that if I need to create some class libraries for this project, can these be created using javascript/html5.

Upvotes: 0

Views: 754

Answers (2)

devhammer
devhammer

Reputation: 1384

To create a reusable JavaScript library for a Windows Store app using HTML and JavaScript is pretty straightforward.

Start by creating a new JavaScript file, with whatever name you like (should be something unique, ideally), say, myMathLib.js.

Start your library using the module pattern, like so (note that the final parens will cause all the code inside the anonymous function to be executed immediately when the file is loaded, which is useful for setting up variables, objects, etc., sometimes referred to as an immediately invoked function expression):

(function () {

// your library code

})();

This will prevent any variables or objects inside the anonymous function from being visible to the outside world unless you explicitly choose to expose it.

Next, add whatever variables and/or functions you wish your library to have. For example, if we were building a math library, we might have an add function:

(function () {

    function add(num1, num2) {
        return num1 + num2;
    }

})();

Obviously, at this point the library isn't very useful, because code outside the library cannot access the add function. So for a Windows Store app, we can use the handy WinJS.Namespace.define function:

(function () {

    WinJS.Namespace.define('myMathLib', {
        addTwoNumbers: add
    });

    function add(num1, num2) {
        return num1 + num2;
    }

})();

To define the namespace and its members, we pass first the name of the namespace by which we'll refer to the members (in this case, 'myMathLib') and then an object with one or more name/value pairs, in which the name is the name by which we'll refer to the member from outside the library, and the value is the internal name. This means we can use a different name internally vs. externally.

Our simple JavaScript library is ready to use. To use it, we need to reference it in an HTML page, as we would with any other JavaScript file, by adding a tag referencing our file:

<script src="/js/myMathLib"></script>

And then in JavaScript for that page, we could use the following to execute our library function:

var num1 = 2, num2 = 40, result;
result = myMathLib.addTwoNumbers(num1, num2)
// result = 42

You can use this technique to build reusable libraries that are as simple or as complex as you need. Another helpful WinJS utility to be aware of is WinJS.Class.define (and the other functions in WinJS.Class), which help in creating types in JavaScript.

Hope this helps!

For more information on Windows Store app development, register for App Builder.

Upvotes: 5

Rich Wagenknecht
Rich Wagenknecht

Reputation: 712

I'm not quite sure what you're asking but...

You can always create javascript libraries that are reusable. Indeed, there are many examples of these (jquery, knockoutjs, prototype, etc).

If you're new to javascript, studying something like jquery can help you learn how to write reusable javascript code.

Upvotes: 0

Related Questions