Javascript:是否存在lua';JavaScript中的s_G?
在lua中,我可以执行以下操作:Javascript:是否存在lua';JavaScript中的s_G?,javascript,object,lua,global,Javascript,Object,Lua,Global,在lua中,我可以执行以下操作: _G.print("Hello world") print("Hello world") 它会将相同的字符串打印到屏幕上。 有没有办法在javascript中访问全局对象?我是说像 _G.console.log === console.log true 这对我的使用案例非常有帮助(我想通过扫描全局对象的更改来防止任何代码注入我的网站)在浏览器中,您可以使用窗口或self。在节点中,您可以使用global。然而,这在总体上不是标准化的,但它是。该方案还展示了一
_G.print("Hello world")
print("Hello world")
它会将相同的字符串打印到屏幕上。
有没有办法在javascript中访问全局对象?我是说像
_G.console.log === console.log
true
这对我的使用案例非常有帮助(我想通过扫描全局对象的更改来防止任何代码注入我的网站)在浏览器中,您可以使用
窗口
或self
。在节点中,您可以使用global
。然而,这在总体上不是标准化的,但它是。该方案还展示了一种获取全局对象的环境无关方式:
我想是
窗口
你在找。。。例如,window.console.log
它在node.js中不起作用(我提供了一个通用的解决方案)
var getGlobal = function () {
// the only reliable means to get the global object is
// `Function('return this')()`
// However, this causes CSP violations in Chrome apps.
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');
};