美文网首页
JS获取顶层对象

JS获取顶层对象

作者: last_edc | 来源:发表于2016-12-15 14:54 被阅读43次

方法一

‘(typeof window !== 'undefined' ? window : (typeof process === 'object' && typeof require === 'function' && typeof global === 'object') ? global : this);’

方法二

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');
};

方法三

使用system.global

// CommonJS的写法
var global = require('system.global')();
// ES6模块的写法
import getGlobal from 'system.global';
const global = getGlobal();

相关文章

  • JS获取顶层对象

    方法一 ‘(typeof window !== 'undefined' ? window : (typeof pr...

  • jQuery对象和DOM对象

    1、dom对象(js对象)使用js的方式获取到的元素就是js对象。 例如: 裤子 入口文件: 1、js对象 $(f...

  • js顶层对象的属性

    顶层对象,在浏览器环境指的是window对象,在Node指的是global对象。ES5之中,顶层对象的属性与全局变...

  • 17.09 冻结对象、获取顶层对象

    九月份收集(待更新) 1.对象完全冻结函数(包括属性) 2.获取顶层对象函数 勉强在任何环境(浏览器、Node、W...

  • ECMAScript 6 - 全局对象的属性

    全局对象是最顶层的对象,在浏览器环境指的是window对象,在Node.js指的是global对象。ES5之中,全...

  • html基础 持续更新

    html js 基础 js 常见获取dom对象的方法 getElementById() getElementByN...

  • OC与JS交互

    ios与js交互,获取webview完整url,title,获取元素并赋值跳转 JS 对象document:属性d...

  • ajax上传图片(FormData形式)

    functionuploadfile(){//js获取文件对象varfileObj=document.getEle...

  • OC与JS互相调用

    OC调用JS 得到JSContext对象。这个对象可以通过webView得到 或者通过 获取。 提供JS内异常的回...

  • 对象

    js对象 js对象是j's的基本数据类型, js中的对象是动态的, 可以新增属性,也可以删除属性。 1.获取时间 ...

网友评论

      本文标题:JS获取顶层对象

      本文链接:https://www.haomeiwen.com/subject/slydmttx.html