我理解"沙盒"这个术语,但我的JS知识有限,无法帮助我理解JS中的沙盒技术。那么,什么是JS中的沙盒技术?除了安全之外,为什么我们需要对JS进行沙盒处理?
沙箱化是创建一个范围,在其中应用程序的其他部分无法操作(除非给予机会)。更具体地说,这通常是一个函数范围,只公开了其中实际发生的有限子集。
建立在沙箱思想上的库之一是YUI3。应用程序的基本单元是一个YUI实例沙箱:
var Y = YUI(); // creates a configurable YUI instance
// Creates a sandbox for one part of your application,
// including the 'node' module.
Y.use('node', function(Z) {
// Z is a YUI instance that's specific to this sandbox.
// Operations inside it are protected from outside code
// unless exposed explicitly. Any modules you request in
// use statement will be separately instanced just for
// this sandbox (in this case, the 'node' module)
//
// That way, if another part of your application decides
// to delete Z.Node (or worse, replace it with a
// malicious proxy of Z.Node) the code you've written
// here won't be affected.
});
$(function(){/*一些代码*/});
,这是你所说的吗? - KumarA
和B
的YUI实例,并且模块B
恰好修改了模块A
(添加了其他属性或更改了其行为),那么我可以稍后创建一个新的YUI实例,只使用模块A
来获取未修改的版本。就我所知,在jQuery中几乎不可能做到这一点。 - Nick Husher