在Javascript中委派函数调用
Javascript中有没有一种方法可以像c#中那样使用委托 c#中的示例 我希望我的Javascript能够让我的主对象在很长一段时间内完成一些事情,并偶尔拍摄一次委托以进行一些更新。所有这些都不需要更改我的类的代码本身来调整网页在Javascript中委派函数调用,javascript,delegates,Javascript,Delegates,Javascript中有没有一种方法可以像c#中那样使用委托 c#中的示例 我希望我的Javascript能够让我的主对象在很长一段时间内完成一些事情,并偶尔拍摄一次委托以进行一些更新。所有这些都不需要更改我的类的代码本身来调整网页 function mainObject() { this.onUpdate = function() { //Potentially the delegate function here } } var a = new mainObject();
function mainObject() {
this.onUpdate = function() { //Potentially the delegate function here
}
}
var a = new mainObject();
a.onUpdate = Delegate {
$(".myText").text("Just got a delegate update");
}
我不知道是否足够清楚。。我还没有找到这方面的资源,所以我想根本没有办法这么做
注意:我不是在这里研究jquery Click delegates事件,而是研究如何在c#中委托函数调用
请告诉我您正在寻找的是一种“观察者模式”,如所述 但是,由于您对jQuery感兴趣,您不需要为自己编写观察者模式。jQuery已经以its的名义实现了一个观察器,可以在jQuery集合上调用它,以便在每次调度本机或自定义事件时触发回调函数 下面是一个例子:
$(function() {
//attach a custom event handler to the document
$(document).on('valueChange', function (evt) {
$(this).find("#s0").text(evt.type);
$(this).find("#s1").text(evt.value);
$(this).find("#s2").text(evt.change);
$(this).find("#s3").text(evt.timestamp).toLocaleString();
});
//customEvent(): a utility function that returns a jQuery Event, with custom type and data properties
//This is necessary for the dispatch an event with data
function customEvent(type, data) {
return $.extend($.Event(type||''), data||{});
};
//randomUpdate(): fetches data and broadcasts it in the form of a 'changeValue' custom event
//(for demo purposes, the data is randomly generated)
function randomUpdate() {
var event = customEvent('valueChange', {
value: (10 + Math.random() * 20).toFixed(2),
change: (-3 + Math.random() * 6).toFixed(2),
timestamp: new Date()
});
$(document).trigger(event);//broadcast the event to the document
}
});
,带有“开始”和“停止”按钮,用于定期“间隔”发送自定义事件
注释
- 在某些情况下,将事件单独广播到四个数据跨度可能更合适
- 在web上,您会发现更方便的
语法。不幸的是,这是jQuery的一个未记录的特性,在v1.9版左右就消失了jQuery.event.trigger({…})
JavaScript中的所有函数(使用
function..
创建)都是“委托”,也就是说,它们是。您可能还对以下内容感兴趣:this
上下文被保留(更像C#delegate)。还请通读相关问题。您是否有一个“自动触发”函数的示例,如c#中所述?如果将Delegate{
替换为function(){
,则显示的JavaScript代码将“正常工作”。例如,在底部:调用a.onUpdate()
将更新文本。如果在C#中谈论事件(不是委托本身),则这是一个不同的概念。显示的示例C#代码不会“自动触发”。不确定是什么意思。您所说的“自动触发”是什么意思委托本身就存在并等待调用。JavaScript中有一个常见的委托模式,但并不清楚这是否是您要寻找的。OP,您应该在问题中提供一个链接,指向此“委托”的定义5年后,我才意识到,$(这个)。find(#s3”).text(evt.timestamp)。tolocalString()
应该是$(这个)。find(#s3”).text(evt.timestamp.tolocalString())
$(function() {
//attach a custom event handler to the document
$(document).on('valueChange', function (evt) {
$(this).find("#s0").text(evt.type);
$(this).find("#s1").text(evt.value);
$(this).find("#s2").text(evt.change);
$(this).find("#s3").text(evt.timestamp).toLocaleString();
});
//customEvent(): a utility function that returns a jQuery Event, with custom type and data properties
//This is necessary for the dispatch an event with data
function customEvent(type, data) {
return $.extend($.Event(type||''), data||{});
};
//randomUpdate(): fetches data and broadcasts it in the form of a 'changeValue' custom event
//(for demo purposes, the data is randomly generated)
function randomUpdate() {
var event = customEvent('valueChange', {
value: (10 + Math.random() * 20).toFixed(2),
change: (-3 + Math.random() * 6).toFixed(2),
timestamp: new Date()
});
$(document).trigger(event);//broadcast the event to the document
}
});
function calledFunc() {
console.log(this.someProp);
}
var myObject = {
someProp : 42,
doSomething : function() {
calledFunc.call(this);
}
}
myObject.doSomething();
// will write 42 to console;