可观察模式在Javascript中是如何工作的?

可观察模式在Javascript中是如何工作的?,javascript,Javascript,我很容易理解它在C#中的工作原理,但在Javascript中我有点困惑。下面是我编写的一个小测试代码: function Lunch(name,price) { var priceChanging = [], priceChanged = []; this.name = function(val) { return name; } this.price = function(val) { if(v

我很容易理解它在C#中的工作原理,但在Javascript中我有点困惑。下面是我编写的一个小测试代码:

  function Lunch(name,price)
  {     
    var priceChanging = [], priceChanged = [];

    this.name = function(val)
    {
        return name;
    }
    this.price = function(val)
    {
        if(val !== price && val !== undefined )
         {
            for(var i = 0; i < priceChanging.length; i++)
            {
                if(!priceChanging[i](this,val))
                {
                    return price;
                }                   
            }
            price = val;
            for(var i = 0; i < priceChanged.length; i++)            
              { 
                priceChanged[i](this);
              }     
         }
        return price;
    }

    this.OnPriceChanging = function(val)    
    {
         priceChanging.push(val);
    }
    this.OnPriceChanged = function(val)     
    {
         priceChanged.push(val);
    }
 }

var order = new Lunch("Fish and Chips",20);

console.log(order.name());
console.log(order.price());

order.OnPriceChanging(function(name,price)
{
    if(price > 30)
    {
        console.log("Price too high");
        return false;
    }
    return true;

});

order.OnPriceChanged(function(name)
{
    console.log("Price changed to: $" + name.price());
});
功能午餐(名称、价格)
{     
var priceChanging=[],priceChanged=[];
this.name=函数(val)
{
返回名称;
}
this.price=函数(val)
{
如果(val!==价格和&val!==未定义)
{
对于(变量i=0;i30)
{
console.log(“价格太高”);
返回false;
}
返回true;
});
order.OnPriceChanged(函数(名称)
{
log(“价格更改为:$”+name.Price());
});
它运行良好,问题是我想能够向自己解释它。我现在不是在调试器前,只是在用记事本。我只是把它想象成.NET,订阅者被放在一个容器中,我只是好奇它在Javascript中是如何工作的

OnPriceChanged和OnPriceChanged函数是否在您添加/更改价格时自动调用?我想我只是对Javascript的松散类型和所有这些感到不舒服


和往常一样,我非常感谢所有传授的知识

这真的很简单。您有两个存储函数的数组:

var priceChanging = [], priceChanged = [];
有两种方法可以将函数推入数组:

this.OnPriceChanging = function(val)    
{
     priceChanging.push(val);
}
this.OnPriceChanged = function(val)     
{
     priceChanged.push(val);
}
order.OnPriceChanging(function(name,price)
{
    if(price > 30)
    {
        console.log("Price too high");
        return false;
    }
    return true;
});

order.OnPriceChanged(function(name)
{
    console.log("Price changed to: $" + name.price());
});
然后将函数推入阵列:

this.OnPriceChanging = function(val)    
{
     priceChanging.push(val);
}
this.OnPriceChanged = function(val)     
{
     priceChanged.push(val);
}
order.OnPriceChanging(function(name,price)
{
    if(price > 30)
    {
        console.log("Price too high");
        return false;
    }
    return true;
});

order.OnPriceChanged(function(name)
{
    console.log("Price changed to: $" + name.price());
});
请注意,如果您不习惯看到匿名函数,那么上面的代码可能会令人困惑。它们完全等同于此:

function priceChangingCallback (name,price)
{
    if(price > 30)
    {
        console.log("Price too high");
        return false;
    }
    return true;
}

function priceChangedCallback (name)
{
    console.log("Price changed to: $" + name.price());
})

order.OnPriceChanging(priceChangingCallback);
order.OnPriceChanged(priceChangedCallback);
您可以看到,数组
priceChanging
priceChanged
现在都应该包含一个函数

OnPriceChanged和OnPriceChanged函数是否在您添加/更改价格时自动调用

不,他们没有。事实上,准确地说,调用的不是
OnPriceChanging
OnPriceChanged
。它是数组
priceChanging
priceChanged
中的函数。他们也不叫自己。你叫他们:

this.price = function(val)
{
    if(val !== price && val !== undefined )
     {
        for(var i = 0; i < priceChanging.length; i++)
        {
            if(!priceChanging[i](this,val)) // <--- you're calling it here!!
            {
                return price;
            }                   
        }
        price = val;
        for(var i = 0; i < priceChanged.length; i++)            
          { 
            priceChanged[i](this); // <-- you're calling it here!!
          }     
     }
    return price;
}
this.price=函数(val)
{
如果(val!==价格和&val!==未定义)
{
对于(变量i=0;i如果(!priceChanging[i](this,val))//可能重复感谢您的回答。让我困惑的是,我只调用了这些函数一次,但当我将order.price()放入x次时,这些函数又被调用了?我没有显式调用它。你说的“那些”是什么意思?正如我上面提到的,您没有调用函数一次,而是在
price
方法内的for循环中调用它们。因此,每次调用
price()
时,您也会调用
priceChanged[I](此)
要习惯的是,函数也是可以像变量一样对待的对象。我不确定C,但这是当今大多数语言的一个正常语言功能。我明白了。这是因为我在C中没有使用过的那些名称和类型。这让人困惑。再次感谢!