Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>To create a reusable JavaScript library for a Windows Store app using HTML and JavaScript is pretty straightforward.</p> <p>Start by creating a new JavaScript file, with whatever name you like (should be something unique, ideally), say, myMathLib.js.</p> <p>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):</p> <pre><code>(function () { // your library code })(); </code></pre> <p>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.</p> <p>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:</p> <pre><code>(function () { function add(num1, num2) { return num1 + num2; } })(); </code></pre> <p>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:</p> <pre><code>(function () { WinJS.Namespace.define('myMathLib', { addTwoNumbers: add }); function add(num1, num2) { return num1 + num2; } })(); </code></pre> <p>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.</p> <p>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:</p> <pre><code>&lt;script src="/js/myMathLib"&gt;&lt;/script&gt; </code></pre> <p>And then in JavaScript for that page, we could use the following to execute our library function:</p> <pre><code>var num1 = 2, num2 = 40, result; result = myMathLib.addTwoNumbers(num1, num2) // result = 42 </code></pre> <p>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 <a href="http://msdn.microsoft.com/en-us/library/windows/apps/hh967790.aspx" rel="nofollow">WinJS.Class.define</a> (and the other functions in WinJS.Class), which help in creating types in JavaScript.</p> <p>Hope this helps!</p> <p>For more information on Windows Store app development, register for <a href="http://bit.ly/gd30tolaunch" rel="nofollow">App Builder</a>.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload