淘汰赛 js 可观察扩展的执行顺序是什么? [英] What order are knockout js observable extensions executed?

查看:25
本文介绍了淘汰赛 js 可观察扩展的执行顺序是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在用于 extending observables 的 Knockoutjs 文档的底部,它指出...><块引用>

在对 observable 的 .extend 方法的一次调用中可以应用多个扩展器.

this.firstName = ko.observable(first).extend({ required: "请输入名字", logChange: "first name" });

在这种情况下,required 和 logChange 扩展器都将针对我们的可观察对象执行.

... 我想知道扩展将按什么顺序执行.它是否总是以相同的顺序执行?什么定义了顺序?

解决方案

如果顺序很重要,可以多次调用extend:

this.firstName = ko.observable(first).extend({ required: "请输入名字" }).extend({ logChange: "名字" });

话虽如此,浏览器通常按照定义对象的相同顺序处理对象的属性.这样你原来的方法就和这个一样了.

At the bottom of the knockoutjs docs for extending observables it states...

More than one extender can be applied in a single call to the .extend method of an observable.

this.firstName = ko.observable(first).extend({ required: "Please enter a first name", logChange: "first name" });

In this case, both the required and logChange extenders would be executed against our observable.

... I want to know what order the extensions will be executed in. Will it always execute in the same order? What definesd the order?

解决方案

If the order is important, you can call extend multiple times:

this.firstName = ko.observable(first)
    .extend({ required: "Please enter a first name" })
    .extend({ logChange: "first name" });

That being said, browsers generally process an object's properties in the same order they were defined. Thus your original method will have the same effect as this one.

这篇关于淘汰赛 js 可观察扩展的执行顺序是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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