九月份收集(待更新)
1.对象完全冻结函数(包括属性)
var constantize = (obj) => {
Object.freeze(obj);
Object.keys(obj).forEach( (key, i) => {
if(typeof obj[key] === 'object'){
constantize( obj[key] );
}
});
};
2.获取顶层对象函数
// 方法一
var getGlobal = function () {
if (typeof self !== 'undefined') { return self; }
if (typeof window !== 'undefined') { return window; }
if (typeof global !== 'undefined') { return global; }
throw new Error('unable to locate global object');
};
// 方法二
(typeof window !== 'undefined'
? window
: (typeof process === 'object' &&
typeof require === 'function' &&
typeof global === 'object')
? global
: this);
勉强在任何环境(浏览器、Node、Web Worker)都能获取到顶层对象的方法,用方法一呗,添加上去就好了