Compartir a través de


WinJS.Namespace.defineWithParent function

Defines a new namespace with the specified name under the specified parent namespace. For more information, see Organizing your code with WinJS.Namespace.

Syntax

var object = WinJS.Namespace.defineWithParent(parentNamespace, name, members);

Parameters

  • parentNamespace
    Type: object

    The parent namespace.

  • name
    Type: string

    The name of the new namespace.

  • members
    Type: object

    The members of the new namespace.

Return value

Type: Object

The newly-defined namespace.

Remarks

If the name contains '.' it is split into parts that are taken to represent a hierarchy. The name following the last '.' will be the leaf namespace node to which the members will be added. Any namespace nodes along the path will be created as needed.

Namespaces should contain only classes, functions, constants, and other namespaces.

Examples

The following code shows how to use this function to define two namespaces: a Robotics namespace with a single Robot class; and a Robotics.Search namespace with a single member findRobot.

(function () {
    "use strict";

    var Robot = WinJS.Class.define(function (name) {
        this.name = name;
    });

    function getAllRobots() {
        return allRobots;
    }

    WinJS.Namespace.define("Robotics", {
        Robot: Robot
    });

    var allRobots = [
        new Robot("mike"),
        new Robot("ellen")
    ];

    function findRobot(robotName) {
        for (var i in allRobots) {
            if (allRobots[i].name == robotName) {
                return allRobots[i];
            }
        }
    }

    WinJS.Namespace.defineWithParent(Robotics, "Search",
        {
            findRobot: findRobot
        })
})();

Requirements

Minimum WinJS version

WinJS 1.0

Namespace

WinJS.Namespace