通过原型将计算的observable添加到构造函数

编程入门 行业动态 更新时间:2024-10-25 23:24:15
本文介绍了通过原型将计算的observable添加到构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在使用Knockout.js 2.0并且我正在尝试通过添加一个计算的observable来扩展我创建的构造函数的原型,但它抛出self.IsSubDomain不是函数。我该如何解决这个错误?是否有另一种方法来扩展构造函数来解决这个问题?

I'm using Knockout.js 2.0 and I'm trying to extend the prototype of the constructor function I've created by adding a computed observable but its throwing up "self.IsSubDomain is not a function". How do I solve this error? Is there another way to extend a constructor function to solve this?

jsfiddle/StrandedPirate/J44S4/3/

注意:我知道我可以在构造函数中定义计算的observable函数的闭包但我正在为淘汰视图模型构建一个自动代码生成器,我需要能够通过prototype属性扩展我的对象。

Note: I know I could define the computed observable inside the constructor function's closure but I'm building an automated code generator for knockout view models and I need to be able to extend my objects through the prototype property.

推荐答案

我还在论坛中对此进行了回答。

这是一种方法( jsFiddle示例 ):

<div data-bind="text: fullDomainName">test</div> <script> function SiteModel(rootUrl, data) { var self = this; self.rootUrl = rootUrl; self.DomainName = ko.observable(data.DomainName); self.IsSubDomain = ko.observable(data.IsSubDomain); self.fullDomainName = koputed(self.fullDomainName, self); } SiteModel.prototype.fullDomainName = function () { if (this.IsSubDomain() && this.DomainName()) { // bombs out here with "self.IsSubDomain is not a function" return this.DomainName() + ".myCompanyWebsite"; } else { return this.DomainName(); } }; var temp = new SiteModel("someurl", { DomainName: "extraCool" }); ko.applyBindings(temp); </script>

我已经在原型中定义了函数并使其成为一个计算的 observable构造函数。

I've defined the function in the prototype and made it a computed observable in the constructor.

这是一种更通用的方式( jsFiddle示例):

Here's a way to do it a more generic way (jsFiddle example):

<div data-bind="text: fullDomainName">test</div> <script> Function.prototypeputed = function() { this.isComputed = true; return this; }; Object.prototype.makeComputeds = function() { for (var prop in this) { if (this[prop] && this[prop].isComputed) { this[prop] = koputed(this[prop], this, {deferEvaluation:true}); } } }; function SiteModel(rootUrl, data) { var self = this; self.rootUrl = rootUrl; self.DomainName = ko.observable(data.DomainName); self.IsSubDomain = ko.observable(data.IsSubDomain); self.makeComputeds(); } SiteModel.prototype.fullDomainName = function () { if (this.IsSubDomain() && this.DomainName()) { // bombs out here with "self.IsSubDomain is not a function" return this.DomainName() + ".myCompanyWebsite"; } else { return this.DomainName(); } }puted(); var temp = new SiteModel("someurl", { DomainName: "extraCool" }); ko.applyBindings(temp); </script>

计算的基础读取函数将通过原型$共享b $ b虽然实际的计算属性不会。我想如果你创建了一些对象然后改变了原型中的函数,可能会产生混乱。新对象将使用新函数,但旧对象不会。

The underlying read function of the computed will be shared via the prototype although the actual computed property won't. I suppose there could be confusion if you created some objects and then changed the function in the prototype. New objects would use the new function, but old objects wouldn't.

由于计算的observable是属性,因此你不应期望能够通过原型将它们添加到现有对象。

Since computed observables are properties, you shouldn't expect to be able to add them to existing objects through the prototype.

更多推荐

通过原型将计算的observable添加到构造函数

本文发布于:2023-10-31 07:52:20,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1545491.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:原型   函数   observable

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!