KnockoutJS:计算与纯计算 [英] KnockoutJS: computed vs. pureComputed

查看:22
本文介绍了KnockoutJS:计算与纯计算的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

KnockoutJS 中的 computedpureComputed 有什么区别?

What's the difference between computed and pureComputed in KnockoutJS?

我可以安全地使用 pureComputed 而不是 computed 吗?

Can I use pureComputed instead of computed safely?

推荐答案

我同意 @Jeroen 的观点,我想从 J. Munro 的 book 对我帮助很大,所以这可能对其他人也有帮助.

I agree with the @Jeroen and I would like to add a short example from J. Munro's book which helped me a lot so this might be helpful for others as well.

首先,pureComputed observables 与计算 observables 非常相似,但有一些性能和内存改进.这个名字是从 Pure function 编程术语中借来的,它意味着任何只使用局部变量的函数可能是纯的,而任何使用非局部变量的函数都可能是不纯的.

First of all, pureComputed observables are quite similar to the computed observables with several performance and memory improvements. The name is borrowed from the Pure function programming term and it means that any function which uses only local variable is potentially pure, whereas any function that uses a non-local variable is potentially impure.

Knockout.js 中的 observable 被区别对待.因此,pureComputed observables 被置于睡眠模式(Knockout 倾斜所有依赖项并在阅读后重新评估内容),并且计算 observables 被置于监听模式(Knockout 在第一次访问之前不断检查值是否是最新的).

The observables in Knockout.js are treated differently. Thus pureComputed observables are placed in a sleeping mode (Knockout inclines all dependencies and re-evaluates the content when after reading) and computed observables are placed into listening mode (Knockout constantly checks whether the value is up-to-date prior to first access).

因此,如果您需要执行其他代码,那么最好使用计算过的 observables.

Therefore, if you need to execute other code, then better to use a computed observables.

function ViewModel() {
     var self = this;

     self.firstName = ko.observable('Arshile');
     self.lastName = ko.observable('Gorky');
     self.pureComputedExecutions = 0;
     self.computedExecutions = 0;

     self.pureComputedFullName = ko.pureComputed(function() {
         // This is NOT recommended 
         self.pureComputedExecutions++;
         return 'Hello ' + self.firstName() + ' ' + self.lastName();
     });
     self.computedFullName = ko.computed(function() {
         self.computedExecutions++;

         return 'Hello ' + self.firstName() + ' ' + self.lastName();
     });
 };
 var viewModel = new ViewModel();
 ko.applyBindings(viewModel);

 alert('Pure computed executions: ' + viewModel.pureComputedExecutions);
 alert('Computed executions: ' + viewModel.computedExecutions);

运行此代码时,会显示两条警报消息,显示调用 pureComputed 和计算函数的次数.由于 pureComputed 处于睡眠模式,因此该函数从未被访问过,计数器将显示 0.与此相反,计算函数在数据绑定时自动求值,导致计数器增加数字并显示 1.

When this code is run, two alert messages are displayed that show the number of times the pureComputed and computed functions are called. Since pureComputed is in sleeping mode then the function has never been accessed, and the counter wil display 0. In contrast to this, the computed function is automatically evaluated on data binding, causing the counter to increase the number and display 1.

这篇关于KnockoutJS:计算与纯计算的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆