[英]JavaScript calling a getter from its function
Trying to do something that in pseudo code would look like this: 尝试使用伪代码执行以下操作:
(function(scope) {
scope.doSomenthin = function() {
if (x === y && this.onfinish) {
// If exists, run onfinish, should return 'fin'
this.onfinish();
}
}
})(scope);
window.scope = window.scope || (window.scope = {});
scope.doSomenthin().onfinish = function(){return 'fin'}
At run time if onfinish
exists, run that function. 如果存在
onfinish
,则在运行时运行该函数。 Tried using getters/setter but at that point it will return undefined. 使用getters / setter尝试过,但此时它将返回undefined。 Setting a timeout works but its not something I wish to do.
设置超时是可行的,但我不希望这样做。
Any other ideas? 还有其他想法吗? Thanks.
谢谢。
I'm not sure if I completely understand the question, but I think what you want comes down to setting the context for the functions you are calling. 我不确定我是否完全理解这个问题,但是我认为您想要的归结为为要调用的函数设置上下文。 Is this what you are after?
这是你所追求的吗?
//create a function that accesses an object's properties and methods with 'this'
var doSomethin = function() {
var result = "nonfinish";
if (this.onfinish) {
// If exists, run onfinish, should return 'fin'
result = this.onfinish();
}
return result;
}
//add an 'onfinish' method to the 'scope' object
scope = {
onfinish: function(){return 'fin'}
}
//run the accessor function in the window context
alert(doSomethin());
//run the accessor function in scope's context
alert(doSomethin.call(scope));
I see several mistakes with your code. 我发现您的代码有几个错误。 This may be the results you are trying to achieve..
这可能是您想要达到的结果。
window.scope = window.scope || (window.scope = {});
scope.onfinish = function(){return 'fin'};
(function(scope) {
scope.doSomenthin = function() {
if (this.onfinish) {
// If exists, run onfinish, should return 'fin'
return this.onfinish();
}
}
})(scope);
alert(scope.doSomenthin());
When you create the temporary scope here you give scope
as a parameter. 在此处创建临时范围时,将
scope
作为参数。 But scope
is not defined yet. 但是
scope
尚未定义。
(function(scope) { scope.doSomenthin = function() { if (x === y && this.onfinish) { // If exists, run onfinish, should return 'fin' this.onfinish(); } } })(scope);
scope.doSomenthin
function doesn't return any value. scope.doSomenthin
函数不返回任何值。 Because of that the value of scope.doSomenthin()
is undifined. scope.doSomenthin()
值未定义。 Therefore with scope.doSomenthin().onfinish = function(){return 'fin'}
you are trying to set a property of undifined. scope.doSomenthin().onfinish = function(){return 'fin'}
您试图设置未定义的属性。 What you want to approach is similar to event-driven programming. 您要采用的方法类似于事件驱动的编程。 Don't just call the function right away, register it as an event handler instead.
不要立即调用该函数,而是将其注册为事件处理程序。 The following pseudo-code only shows my idea.
以下伪代码仅显示了我的想法。 It's not complete
还不完整
//register the function here, instead of calling it immediately
event = document.createEvent("HTMLEvents");
event.initEvent("myEvent", true, true);
document.addEventListener("myEvent", function(e) {
e.scope.doSomenthin = function() {
if (this.onfinish) {
// If exists, run onfinish, should return 'fin'
return this.onfinish();
}
}
});
......
//call the handler to handle the below event
window.scope = window.scope || (window.scope = {});
scope.doSomenthin().onfinish = function(){return 'fin'}
event.scope = scope;
document.body.dispatchEvent(event);
The above code is kind of silly. 上面的代码有点愚蠢。 You have to design where to put and trigger the events.
您必须设计放置和触发事件的位置。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.