Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/369.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Javascript中委派函数调用_Javascript_Delegates - Fatal编程技术网

在Javascript中委派函数调用

在Javascript中委派函数调用,javascript,delegates,Javascript,Delegates,Javascript中有没有一种方法可以像c#中那样使用委托 c#中的示例 我希望我的Javascript能够让我的主对象在很长一段时间内完成一些事情,并偶尔拍摄一次委托以进行一些更新。所有这些都不需要更改我的类的代码本身来调整网页 function mainObject() { this.onUpdate = function() { //Potentially the delegate function here } } var a = new mainObject();

Javascript中有没有一种方法可以像c#中那样使用委托

c#中的示例

我希望我的Javascript能够让我的主对象在很长一段时间内完成一些事情,并偶尔拍摄一次委托以进行一些更新。所有这些都不需要更改我的类的代码本身来调整网页

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.event.trigger({…})
    语法。不幸的是,这是jQuery的一个未记录的特性,在v1.9版左右就消失了

虽然最初的问题是通过解决根本问题(观察者模式)来解决的,但是有一种方法可以在JavaScript中实现委托

C#委托模式在使用上下文绑定的本机JavaScript中可用。JavaScript中的上下文绑定是通过.call方法完成的。函数将在第一个参数给定的上下文中调用。 例如:


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;