Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7854129
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T19:57:00+00:00 2026-06-02T19:57:00+00:00

I’m using Knockout.js 2.0 and I’m trying to extend the prototype of the constructor

  • 0

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?

http://jsfiddle.net/StrandedPirate/J44S4/3/

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T19:57:02+00:00Added an answer on June 2, 2026 at 7:57 pm

    I also answered this in the forum.

    Here’s one way to do it (jsFiddle example):

    <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 = ko.computed(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.com";
        }
        else {
            return this.DomainName();
       }
    }; 
    
    var temp = new SiteModel("someurl", { DomainName: "extraCool" });
    
    ko.applyBindings(temp);
    </script>
    

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

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

    <div data-bind="text: fullDomainName">test</div>
    <script>
    Function.prototype.computed = function() {
        this.isComputed = true;
        return this;
    };
    Object.prototype.makeComputeds = function() {
        for (var prop in this) {
            if (this[prop] && this[prop].isComputed) {
                this[prop] = ko.computed(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.com";
        }
        else {
            return this.DomainName();
       }
    }.computed();
    
    var temp = new SiteModel("someurl", { DomainName: "extraCool" });
    
    ko.applyBindings(temp);
    </script>
    

    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.

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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I want to construct a data frame in an Rcpp function, but when I
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I am trying to understand how to use SyndicationItem to display feed which is
I am reading a book about Javascript and jQuery and using one of the
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I am trying to render a haml file in a javascript response like so:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.