2

我有一些这样的代码:

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);
      ...
  }
}

如何将函数 1 和函数 2 “提取”为 A 和 B 可以共享的函数?

4

4 回答 4

5

这正是原型适合的地方:

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

话虽如此,就个人而言,我更喜欢定期定义我的构造函数:

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

而您的代码将匿名函数分配给变量,这意味着构造函数没有名称:

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

这没什么大不了的,但只是让你知道......


从全局(或任何其他)范围引用方法:

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

闭包仍然可以访问(暴露),但要非常小心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

另一种可能性是“借用”一个函数:

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

同样,要小心:在这种情况下,this日志B,但self仍然引用A!您可以建立某些“安全网”

    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);
    };

但老实说,您可能会很好地阅读 this 运算符以掌握所有这些引用技巧。一旦你掌握了基础知识这并不难。还要检查callapply方法以获取有关如何“共享/借用”方法的更多详细信息

于 2012-10-01T14:47:42.557 回答
0

您可以通过将其拉出该对象来更改函数的范围。

var function1 = function() {

}

var function2 = function() {

}

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


  self.function1 = ko.computed(function1)

  self.function2 = ko.computed(function2)
}
于 2012-10-01T14:36:43.477 回答
0

您可以尝试使用 underscore.js 扩展功能来进行继承。

于 2013-08-04T19:56:37.503 回答
0
    var AB=function(){
    self=this;
    self.function1=function(){
                   //----something------
                  }
    self.function1=function(){
                   //----something------
                  }

   }


     var A=function(){
     var self=this;
     AB.apply(self,arguments);
     } 

    var B=function(){
     var self=this;
     AB.apply(self,arguments);
        } 
于 2013-08-05T22:15:31.537 回答