0
我讀的article有關私營/ protected成員「仿真」 與JavaScript(我知道這是不是一個最佳實踐但它是研究目的)在javascript中使用閉包之間的變量是否是線程安全的?
例如,在這個代碼中,我們已經:
var Class = (function() { // Open closure
var caller = null;
//[...]
var mayAccessWrapped = false;
function wrapmethod(method) {
mayAccessWrapped = true;
if (method.__getWrappedMethod) {
method = method.__getWrappedMethod();
}
mayAccessWrapped = false;
var wrapped = function wrapper() {
var prevCaller = caller;
caller = wrapper;
var returns;
try {
returns = method.apply(this, arguments);
}
finally {
caller = prevCaller;
}
return returns;
};
wrapped.__getWrappedMethod = function() {
if (mayAccessWrapped) { return method; }
throw "Error: only the wrapping function may access the wrapped method";
}
return wrapped;
}
//[...]
return Class;
})(); // End Class closure
「包裝」 方法是從同一個對象多次使用和它使用調用者變量通過「調用」方法「注入」有關「調用者」的信息(alternative to deprecated arguments.callee)。
但是,在一個併發的範圍內,這種方式線程安全嗎?是否有可能另一種方法可以改變調用者的值變量使數據一致性失效?
Javascript沒有線程。 – SLaks
不完全正確,它依賴於引擎:http://jxcore.com/home/ – Joseph
從鏈接到的引擎的文檔:*「JXcore中的每個實例都稱爲子實例。子實例完全與主實例和其他子實例分離,因爲它們都運行在不同的V8上下文/隔離區中,這意味着它們不能共享或直接共享對象,變量等。「* –