这是可能的还是我在这里吠错了树?
var data = 'one';
function fnc(){
this.out = function(){
return data;
}
}
var instance = new fnc();
alert(instance.out);
data = 'two';
alert(instance.out);
// I know that this would achieve that, but that's not what I would like to know.
alert(instance.out());
data = 'two';
alert(instance.out());
更新:
fnc 应该表示的对象实际上是一个 Sarissa dom 文档。这是 fnc() 的更详细版本,dom_doc()。下面接受的答案已集成到下面的函数中。
function get_doc(dom_node) {
var doc;
if (navigator.userAgent.indexOf("MSIE") >= 0) {
doc = new ActiveXObject("Msxml2.DOMDocument.3.0");
doc.loadXML(document.getElementById(dom_node).text);
}
else {
doc = Sarissa.getDomDocument();
doc = (new DOMParser()).parseFromString(document.getElementById(dom_node).textContent, "text/xml");
// runs XSLTProcessor in modern browsers as if it was trasformNode
doc.transformNode = function (stylesheet) {
var processor = new XSLTProcessor();
processor.importStylesheet(stylesheet);
return new XMLSerializer().serializeToString(processor.transformToDocument(this));
}
// allows modern browsers to extract xml the way the legacy IEs did
var getXML = {};
getXML.toString = function(){
return new XMLSerializer().serializeToString(doc);
};
doc.xml = getXML;
}
return doc;
}
演示: JSFIDDLE