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 8623197
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T07:13:37+00:00 2026-06-12T07:13:37+00:00

I have some code like this: var A = function(a,b,c) { var self =

  • 0

I have some code like this:

var A = function(a,b,c) {
  var self = this;
  self.a = ko.observable(a);
  ... 


  self.function1 = ko.computed(function () {
      dothing(a);
      ...
  } 

  self.function2 = ko.computed(function () {
      dothing(b);
      ...
  }
}

var B = function(a,b,c,d) {
    var self = this;
  self.a = ko.observable(a);
  ... 


  self.function1 = ko.computed(function () {
      dothing(a);
      ...
  } 

  self.function2 = ko.computed(function () {
      dothing(b);
      ...
  }
}

How can I “extract” function1 and function2 to a function that A and B can share?

  • 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-12T07:13:38+00:00Added an answer on June 12, 2026 at 7:13 am

    That’s just where prototypes fit in:

    function AB()
    {};//empty object
    AB.prototype.function1 = function()
    {
        var self = this;//doesn't have access to self, but `this` will point to either A or B
        //do stuff
    };
    var A = function()
    {
        var self = this;//constructor
    }
    var B = function()
    {
        var self = this;//constructor
    }
    A.prototype = new AB;
    A.prototype.constructor = A;
    B.prototype = new AB;
    B.prototype.constructor = B;
    //marginally shorter:
    A.prototype = B.prototype = new AB;
    A.prototype.constructor = A;
    B.prototype.constructor = B;
    //instances:
    var foo = new A();
    var bar = new B();
    console.log(foo.function1 === bar.function1);//logs true
    

    Having said that, personally, I prefer to define my constructors regularly:

    function A()
    {
        var self = this;
    }
    foo = new A();
    console.log(Object.getPrototypeOf(foo).constructor.name);//logs A
    

    Whereas your code assigns an anonymous function to a variable, which means that the constructor doesn’t have a name:

    foo = new A();
    console.log(Object.getPrototypeOf(foo).constructor.name);//logs ''
    

    It’s not that big of a deal, but just so you know…


    Reference a method from the global (or any other) scope:

    var virtualNewFunction = new A();//create object
    virtualNewFunction = virtualNewFunction.function1;//virtualNewFunction now references function1
    virtualNewFunction();
    

    The closure will be accessible (exposed), still, but be very careful with this:

    A = function()
    {
        var self = this;
        this.function1 = function()
        {
            console.log(this);
            console.log(self);
        };
    }
    foo = new A();
    foo.function1();//logs A twice
    foo = foo.function1
    foo();//logs this -> window! self => A
    

    An other possibility is “borrowing” a function:

    A = function()
    {
        var self = this;
        this.function1 = function()
        {
            console.log(this);
            console.log(self);
        };
    }
    B = function()
    {//no method
        var self = this;
    }
    foo = new A();
    bar = new B();
    foo.function1.apply(bar,[]);//call as though function1 was method of B
    

    Again, be careful: in this case this logs B, but self still references A! You could build in certain “safety nets”:

        this.function1 = function()
        {
            self = this !== window && this !== self ? this : self;//redefine self to current caller object, unless it's window 
            console.log(this);
            console.log(self);
        };
    

    But honestly, you might do well reading up on the this operator to grasp all this referencing trickery. It’s not that hard once you get the basics. Also check the call and apply methods for more details on how to “share/borrow” methods

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

Sidebar

Related Questions

I have some code like this: var content = document.getElementById('myDivId'); function MyFunction() { alert(content.style.height);
I have some code that looks like this: var MyObject = function () {
I have some code like this: function switch_tabs(obj) { $('.tab-content').hide(); $('.tabs a').removeClass(selected); var id
I have some code like this: var User = function () { _.bindAll(this) this.UserList
I have some code like this: var mySelect = document.getElementById(mySelect); mySelect.innerHTML = <option>Loading...</option>; $.get(ajaxPage.php,
I have some code that looks like this: foreach(var obj in collection) { try
I have some LINQ code that generates a list of strings, like this: var
I have javascript code some thing like this -- var count=3; var pl=new Array(count);
I have some code like this: doDatabaseFetch { ... @synchronized(self) { ... } }
I have some code like this: if (condition) { var variable = blah; }

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.