Path: blob/trunk/third_party/closure/goog/reflect/reflect.js
2868 views
// Copyright 2009 The Closure Library Authors. All Rights Reserved.1//2// Licensed under the Apache License, Version 2.0 (the "License");3// you may not use this file except in compliance with the License.4// You may obtain a copy of the License at5//6// http://www.apache.org/licenses/LICENSE-2.07//8// Unless required by applicable law or agreed to in writing, software9// distributed under the License is distributed on an "AS-IS" BASIS,10// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.11// See the License for the specific language governing permissions and12// limitations under the License.1314/**15* @fileoverview Useful compiler idioms.16*17* @author [email protected] (John Lenz)18*/1920goog.provide('goog.reflect');212223/**24* Syntax for object literal casts.25* @see http://go/jscompiler-renaming26* @see https://goo.gl/CRs09P27*28* Use this if you have an object literal whose keys need to have the same names29* as the properties of some class even after they are renamed by the compiler.30*31* @param {!Function} type Type to cast to.32* @param {Object} object Object literal to cast.33* @return {Object} The object literal.34*/35goog.reflect.object = function(type, object) {36return object;37};3839/**40* Syntax for renaming property strings.41* @see http://go/jscompiler-renaming42* @see https://goo.gl/CRs09P43*44* Use this if you have an need to access a property as a string, but want45* to also have the property renamed by the compiler. In contrast to46* goog.reflect.object, this method takes an instance of an object.47*48* Properties must be simple names (not qualified names).49*50* @param {string} prop Name of the property51* @param {!Object} object Instance of the object whose type will be used52* for renaming53* @return {string} The renamed property.54*/55goog.reflect.objectProperty = function(prop, object) {56return prop;57};5859/**60* To assert to the compiler that an operation is needed when it would61* otherwise be stripped. For example:62* <code>63* // Force a layout64* goog.reflect.sinkValue(dialog.offsetHeight);65* </code>66* @param {T} x67* @return {T}68* @template T69*/70goog.reflect.sinkValue = function(x) {71goog.reflect.sinkValue[' '](x);72return x;73};747576/**77* The compiler should optimize this function away iff no one ever uses78* goog.reflect.sinkValue.79*/80goog.reflect.sinkValue[' '] = goog.nullFunction;818283/**84* Check if a property can be accessed without throwing an exception.85* @param {Object} obj The owner of the property.86* @param {string} prop The property name.87* @return {boolean} Whether the property is accessible. Will also return true88* if obj is null.89*/90goog.reflect.canAccessProperty = function(obj, prop) {9192try {93goog.reflect.sinkValue(obj[prop]);94return true;95} catch (e) {96}97return false;98};99100101/**102* Retrieves a value from a cache given a key. The compiler provides special103* consideration for this call such that it is generally considered side-effect104* free. However, if the {@code opt_keyFn} or {@code valueFn} have side-effects105* then the entire call is considered to have side-effects.106*107* Conventionally storing the value on the cache would be considered a108* side-effect and preclude unused calls from being pruned, ie. even if109* the value was never used, it would still always be stored in the cache.110*111* Providing a side-effect free {@code valueFn} and {@code opt_keyFn}112* allows unused calls to {@code goog.reflect.cache} to be pruned.113*114* @param {!Object<K, V>} cacheObj The object that contains the cached values.115* @param {?} key The key to lookup in the cache. If it is not string or number116* then a {@code opt_keyFn} should be provided. The key is also used as the117* parameter to the {@code valueFn}.118* @param {function(?):V} valueFn The value provider to use to calculate the119* value to store in the cache. This function should be side-effect free120* to take advantage of the optimization.121* @param {function(?):K=} opt_keyFn The key provider to determine the cache122* map key. This should be used if the given key is not a string or number.123* If not provided then the given key is used. This function should be124* side-effect free to take advantage of the optimization.125* @return {V} The cached or calculated value.126* @template K127* @template V128*/129goog.reflect.cache = function(cacheObj, key, valueFn, opt_keyFn) {130var storedKey = opt_keyFn ? opt_keyFn(key) : key;131132if (Object.prototype.hasOwnProperty.call(cacheObj, storedKey)) {133return cacheObj[storedKey];134}135136return (cacheObj[storedKey] = valueFn(key));137};138139140