Domanda

How can I make an enumeration in WinJS?

I tried to make a class like this for example:

(function () {
"use strict";

var TaskEnum = WinJS.Class.define(
null,
{},
// The set of static members.
{
    SUPERVISION: 1,
    DIG: 2,
    MAP: 3,
});


var Task = new TaskEnum();

WinJS.Namespace.define("ENUMS", {
    TASK: {
        get: function () {
            return Task;
        }
    }
});


})();

But when I call ENUMS.TASK.DIG then DIG is undefined.

I even tried just this:

WinJS.Namespace.define("ENUMS", {
    TASK:  { SUPERVISION: 1, DIG: 2, MAP: 3, }
});

But when I call ENUMS.TASK.DIG then DIG is still undefined.

It seems like the Namespace.define does not work in that way?

È stato utile?

Soluzione

Ohk, If i get you exact then , you want to define Enumeration in JavaScript? right ? For this case I don't think if it is done with WinJs or not on Windows Store App, but I have two Suggestions as my answer :

1 ) Use C# as Runtime Component to perform This operations, by defining a function in c# and simply call it on your JavaScript.

See ,Creating a simple component in C#.

2 ) Enumerated types in JavaScript

Or See : WinJS.UI Namespace
http://207.46.99.208/pl-pl/library/windows/apps/br229782#enums_section

Altri suggerimenti

I might not understand what you're hoping to get out of the Namespace definition, but not just use core JavaScript?

window.ENUMS = {
    TASK: {
        SUPERVISION: 1,
        DIG: 2,
        MAP: 3
    }
};

Then use it like you were trying to:

callSomeFunction(ENUMS.TASK.DIG);

Note that if you can restrict users to IE11 (not so difficult in Win8 apps) then you can make use of JavaScript constants through const as well.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top