react / react-0.13.3 / examples / basic-commonjs / node_modules / reactify / node_modules / react-tools / node_modules / commoner / node_modules / private / private.js
81164 views"use strict";12var originalObject = Object;3var originalDefProp = Object.defineProperty;4var originalCreate = Object.create;56function defProp(obj, name, value) {7if (originalDefProp) try {8originalDefProp.call(originalObject, obj, name, { value: value });9} catch (definePropertyIsBrokenInIE8) {10obj[name] = value;11} else {12obj[name] = value;13}14}1516// For functions that will be invoked using .call or .apply, we need to17// define those methods on the function objects themselves, rather than18// inheriting them from Function.prototype, so that a malicious or clumsy19// third party cannot interfere with the functionality of this module by20// redefining Function.prototype.call or .apply.21function makeSafeToCall(fun) {22if (fun) {23defProp(fun, "call", fun.call);24defProp(fun, "apply", fun.apply);25}26return fun;27}2829makeSafeToCall(originalDefProp);30makeSafeToCall(originalCreate);3132var hasOwn = makeSafeToCall(Object.prototype.hasOwnProperty);33var numToStr = makeSafeToCall(Number.prototype.toString);34var strSlice = makeSafeToCall(String.prototype.slice);3536var cloner = function(){};37function create(prototype) {38if (originalCreate) {39return originalCreate.call(originalObject, prototype);40}41cloner.prototype = prototype || null;42return new cloner;43}4445var rand = Math.random;46var uniqueKeys = create(null);4748function makeUniqueKey() {49// Collisions are highly unlikely, but this module is in the business of50// making guarantees rather than safe bets.51do var uniqueKey = internString(strSlice.call(numToStr.call(rand(), 36), 2));52while (hasOwn.call(uniqueKeys, uniqueKey));53return uniqueKeys[uniqueKey] = uniqueKey;54}5556function internString(str) {57var obj = {};58obj[str] = true;59return Object.keys(obj)[0];60}6162// External users might find this function useful, but it is not necessary63// for the typical use of this module.64defProp(exports, "makeUniqueKey", makeUniqueKey);6566// Object.getOwnPropertyNames is the only way to enumerate non-enumerable67// properties, so if we wrap it to ignore our secret keys, there should be68// no way (except guessing) to access those properties.69var originalGetOPNs = Object.getOwnPropertyNames;70Object.getOwnPropertyNames = function getOwnPropertyNames(object) {71for (var names = originalGetOPNs(object),72src = 0,73dst = 0,74len = names.length;75src < len;76++src) {77if (!hasOwn.call(uniqueKeys, names[src])) {78if (src > dst) {79names[dst] = names[src];80}81++dst;82}83}84names.length = dst;85return names;86};8788function defaultCreatorFn(object) {89return create(null);90}9192function makeAccessor(secretCreatorFn) {93var brand = makeUniqueKey();94var passkey = create(null);9596secretCreatorFn = secretCreatorFn || defaultCreatorFn;9798function register(object) {99var secret; // Created lazily.100101function vault(key, forget) {102// Only code that has access to the passkey can retrieve (or forget)103// the secret object.104if (key === passkey) {105return forget106? secret = null107: secret || (secret = secretCreatorFn(object));108}109}110111defProp(object, brand, vault);112}113114function accessor(object) {115if (!hasOwn.call(object, brand))116register(object);117return object[brand](passkey);118}119120accessor.forget = function(object) {121if (hasOwn.call(object, brand))122object[brand](passkey, true);123};124125return accessor;126}127128defProp(exports, "makeAccessor", makeAccessor);129130131