public
Created

WinJS.Class from base.js in the Metro Web App Template

  • Download Gist
WinJS.Class.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
/*
© Microsoft. All rights reserved.
*/
 
(function (global, undefined) {
function initializeProperties(target, members) {
var keys = Object.keys(members);
var properties;
var i, len;
for (i = 0, len = keys.length; i < len; i++) {
var key = keys[i];
var enumerable = key.charCodeAt(0) !== /*_*/95;
var member = members[key];
if (member && typeof member === 'object') {
if (member.value !== undefined || typeof member.get === 'function' || typeof member.set === 'function') {
if (member.enumerable === undefined) {
member.enumerable = enumerable;
}
properties = properties || {};
properties[key] = member;
continue;
}
}
if (!enumerable) {
properties = properties || {};
properties[key] = { value: member, enumerable: enumerable, configurable: true, writable: true }
continue;
}
target[key] = member;
}
if (properties) {
Object.defineProperties(target, properties);
}
}
(function (rootNamespace) {
// Create the rootNamespace in the global namespace
if (!global[rootNamespace]) {
global[rootNamespace] = Object.create(Object.prototype);
}
// Cache the rootNamespace we just created in a local variable
var _rootNamespace = global[rootNamespace];
if (!_rootNamespace.Namespace) {
_rootNamespace.Namespace = Object.create(Object.prototype);
}
function defineWithParent(parentNamespace, name, members) {
/// <summary locid="1">
/// Defines a new namespace with the specified name, under the specified parent namespace.
/// </summary>
/// <param name="parentNamespace" type="Object" locid="2">
/// The parent namespace which will contain the new namespace.
/// </param>
/// <param name="name" type="String" locid="3">
/// Name of the new namespace.
/// </param>
/// <param name="members" type="Object" locid="4">
/// Members in the new namespace.
/// </param>
/// <returns locid="5">
/// The newly defined namespace.
/// </returns>
var currentNamespace = parentNamespace,
namespaceFragments = name.split(".");
for (var i = 0, len = namespaceFragments.length; i < len; i++) {
var namespaceName = namespaceFragments[i];
if (!currentNamespace[namespaceName]) {
Object.defineProperty(currentNamespace, namespaceName,
{ value: {}, writable: false, enumerable: true, configurable: true }
);
}
currentNamespace = currentNamespace[namespaceName];
}
if (members) {
initializeProperties(currentNamespace, members);
}
return currentNamespace;
};
function define(name, members) {
/// <summary locid="6">
/// Defines a new namespace with the specified name.
/// </summary>
/// <param name="name" type="String" locid="7">
/// Name of the namespace. This could be a dot-separated nested name.
/// </param>
/// <param name="members" type="Object" locid="4">
/// Members in the new namespace.
/// </param>
/// <returns locid="5">
/// The newly defined namespace.
/// </returns>
return defineWithParent(global, name, members);
}
// Establish members of the "WinJS.Namespace" namespace
Object.defineProperties(_rootNamespace.Namespace, {
defineWithParent: { value: defineWithParent, writable: true, enumerable: true },
define: { value: define, writable: true, enumerable: true }
});
})("WinJS");
(function (WinJS) {
function define(constructor, instanceMembers, staticMembers) {
/// <summary locid="8">
/// Defines a class using the given constructor and with the specified instance members.
/// </summary>
/// <param name="constructor" type="Function" locid="9">
/// A constructor function that will be used to instantiate this class.
/// </param>
/// <param name="instanceMembers" type="Object" locid="10">
/// The set of instance fields, properties and methods to be made available on the class.
/// </param>
/// <param name="staticMembers" type="Object" locid="11">
/// The set of static fields, properties and methods to be made available on the class.
/// </param>
/// <returns type="Function" locid="12">
/// The newly defined class.
/// </returns>
constructor = constructor || function () { };
if (instanceMembers) {
initializeProperties(constructor.prototype, instanceMembers);
}
if (staticMembers) {
initializeProperties(constructor, staticMembers);
}
return constructor;
}
function derive(baseClass, constructor, instanceMembers, staticMembers) {
/// <summary locid="13">
/// Uses prototypal inheritance to create a sub-class based on the supplied baseClass parameter.
/// </summary>
/// <param name="baseClass" type="Function" locid="14">
/// The class to inherit from.
/// </param>
/// <param name="constructor" type="Function" locid="9">
/// A constructor function that will be used to instantiate this class.
/// </param>
/// <param name="instanceMembers" type="Object" locid="10">
/// The set of instance fields, properties and methods to be made available on the class.
/// </param>
/// <param name="staticMembers" type="Object" locid="11">
/// The set of static fields, properties and methods to be made available on the class.
/// </param>
/// <returns type="Function" locid="12">
/// The newly defined class.
/// </returns>
if (baseClass) {
constructor = constructor || function () { };
var basePrototype = baseClass.prototype;
constructor.prototype = Object.create(basePrototype);
Object.defineProperty(constructor.prototype, "_super", { value: basePrototype });
Object.defineProperty(constructor.prototype, "constructor", { value: constructor });
if (instanceMembers) {
initializeProperties(constructor.prototype, instanceMembers);
}
if (staticMembers) {
initializeProperties(constructor, staticMembers);
}
return constructor;
} else {
return define(constructor, instanceMembers, staticMembers);
}
}
function mix(constructor) {
/// <summary locid="15">
/// Defines a class using the given constructor and the union of the set of instance members
/// specified by all the mixin objects. The mixin parameter list can be of variable length.
/// </summary>
/// <param name="constructor" locid="9">
/// A constructor function that will be used to instantiate this class.
/// </param>
/// <returns locid="12">
/// The newly defined class.
/// </returns>
constructor = constructor || function () { };
var i, len;
for (i = 0, len = arguments.length; i < len; i++) {
initializeProperties(constructor.prototype, arguments[i]);
}
return constructor;
}
// Establish members of "WinJS.Class" namespace
WinJS.Namespace.define("WinJS.Class", {
define: define,
derive: derive,
mix: mix
});
})(WinJS);
})(this);

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.