简体   繁体   English

如何在JS中公开成员变量的公共方法

[英]how to expose public methods of member variables in JS

I have a class 我上课了

function A()
{
   var that = this;
   var b = new B(); 
   this.setBSize = function(newSize)
   {
      b.setSize(newSize);
   }


};

function B()
{
   var that = this;

   this.setSize = function(newSize)
   {
   ...
   }
}

a = new A();
a.setBSize(5);

How do I avoid writing the setBSize method? 如何避免编写setBSize方法? How do I expose the public methods of b automatically? 如何自动公开b的公共方法? I want to do the call like this 我想这样打电话

a.setSize(5);

Also I need a reference to new B(); 我还需要一个new B();的引用new B(); which is b inside A() 哪个是A()里面A() b A()

You could always set the prototype of A to B if you want to inherit all the methods in B 你总是可以设置的原型AB ,如果你想继承所有方法B

function A() {
    var that = this;
};

function B() {
    var that = this;

    this.setSize = function (newSize) {
        console.log(newSize); // 5
    }
}

A.prototype = new B();

a = new A();
a.setSize(5);

FIDDLE 小提琴

In jQuery : $.extend(that, new B()); jQuery$.extend(that, new B()); In angular : angular.extend(that, new B()); angularangular.extend(that, new B());

function A()
{
   var that = this;
   $.extend(that, new B());
};

function B()
{
   var that = this;

   this.setSize = function(newSize)
   {
   ...
   }
}

a = new A();
a.setSize(5);

And if you want to use any private variables in B() class define them as var someVar , and all public (overridable) variables as that.somePublicVar 如果你想在B()类中使用任何private变量,则将它们定义为var someVar ,将所有公共(可that.somePublicVar )变量定义为that.somePublicVar

You can use call method for this: 你可以使用call方法:

function A() {
    var that = this;
    B.call(this);
};

function B() {
    var that = this;
    this.setSize = function (newSize) {
        this.size = newSize;
    }
}

var a = new A();
a.setSize(5);

Basically you invoke B in context of A , and what happens is that all own properties of the B instance are going to be assigned to this which is A instance. 基本上你调用B在上下文A ,什么情况是,所有的自身属性B实例都将被分配到this这是A实例。 This pattern is called constructor or methods borrowing. 这种模式称为构造函数或方法借用。

You should utilize prototyping. 你应该利用原型。

make a constructor which shares the function among all the classes(objects): 创建一个在所有类(对象)之间共享函数的构造函数:

var myConstructor = function(newSize){

   this.setSize = function(newSize)
   {
   ...
   }
}

Now you do instanciation: 现在你做instanciation:

var a = new myConstructor(someSize);

var b = new myConstrucotr(someSize);

Now with this change a.setSize() is the same as b.setSize() 现在有了这个改变, a.setSize()b.setSize()

Using prototype for inheriting the method setSize and discarding all the this and that code. 使用prototype继承方法setSize并丢弃所有thisthat代码。

function B() {
};
function A() {
    B.call(this);
};

B.prototype.setSize = function(newSize) {
    console.log(newSize);
}

A.prototype = Object.create(B.prototype);
A.prototype.constructor = A;
var a = new A();
a.setSize(5);               // 5
console.log(a instanceof A);// true
console.log(a instanceof B);// true

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM