Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JavaScript:公共方法和原型_Javascript_Oop_Prototype_Public Method - Fatal编程技术网

JavaScript:公共方法和原型

JavaScript:公共方法和原型,javascript,oop,prototype,public-method,Javascript,Oop,Prototype,Public Method,我不完全确定如何在JS中实现OOP概念 我有一个完全在构造函数中声明的类: function AjaxList(settings) { // all these vars are of dubious necessity... could probably just use `settings` directly var _jq_choice_selector = settings['choice_selector']; var _jq_chosen_list = se

我不完全确定如何在JS中实现OOP概念

我有一个完全在构造函数中声明的类:

function AjaxList(settings)
{

    // all these vars are of dubious necessity... could probably just use `settings` directly
    var _jq_choice_selector = settings['choice_selector'];
    var _jq_chosen_list = settings['chosen_list'];
    var _cb_onRefresh = settings['on_refresh'];
    var _url_all_choices = settings['url_choices'];
    var _url_chosen = settings['url_chosen'];
    var _url_delete_format = settings['url_delete_format'];

    var jq_choice_selector_form = _jq_choice_selector.closest("form");
    if (DEBUG && jq_choice_selector_form.length != 1)
    {
        throw("There was an error selecting the form for the choice selector.");
    }

    function refresh()
    {
        _updateChoicesSelector();
        _updateChosenList();
        _cb_onRefresh();
    };

    AjaxList.prototype.refresh = refresh; // will this be called on all AjaxLists, or just the instance used to call it?
    // AjaxList.refresh = refresh; // will this be called on all AjaxLists, or just the instance used to call it?

    // ...
}
AjaxList有多个实例。当我对其中一个列表调用
refresh()
时,我只希望该列表能够自我刷新。在下列情况下:

term_list = AjaxList(settings);
term_list.refresh();
refresh()
调用似乎会使所有AjaxList自我刷新。正确的方法是什么


如果有什么不同的话,我使用的是jQuery。

您不应该在构造函数中重新定义原型函数。 如果要创建特权函数,请使用this.methodname=。。。来自构造函数

function AjaxList() {
  var privateVar = 0;
  function privateFunction() {
    //...
  }
  //create a refresh function just for this instance of the AjaxList
  this.refresh = function() {
    //privileged function, it can access the 'privateVar & privateFunction'
    privateVar++;
  }
}
//public functions that don't need access to the private variables/functions
AjaxList.prototype.publicFunction=function() {

};
此外,如果要创建适当的对象,则需要更改

term_list = AjaxList(settings);

鉴于这种结构,您应该能够调用:

var ajaxList = new AjaxList(settings);
ajaxList.refresh(); // etc.
我正在使用jQuery,如果它有什么好处的话 差别

不,没有。请看我的回答:

我有一门课完全是免费的 在其构造函数中声明

Javascript中没有类。忘了他们吧。你真的需要学习这种语言的一些基础知识才能使用它们。它不是Java,尽管它看起来很相似

如果您有一个构造函数,它将创建一个实例。共享方法将位于原型链中,只有特定于实例的数据会直接进入带有此关键字的函数中

因此,对象的基本概念如下所示:

// constructor of an instance
function MyObject( param1, param2 ) {
  this.param1 = param1;
  this.param2 = param2;
  this.param3 = 32;
  return this; // [optional]
}

// Public methods can be called by any instance.
// Instances share their prototype object.
// The this keyword always points to the current
// instance that calls the method.
MyObject.prototype.sum = function() {
  return this.param1 + this.param2 + this.param3;
}

// refresh should be a shared method, since it
// does the same thing on every instance
MyObject.prototype.refresh = function() {
  // do the refresh
  // ...
}

这个概念的功能是内存中只有一个刷新功能。它可以处理任何情况。此外,如果另一个对象从MyObject继承,刷新功能将被继承。但是在内存中仍然有一个共享的刷新功能。它可以处理任何父实例或子实例。

您对该语言有一些严重的误解(javascript、jQuery、class)。看我的答案,看清楚它们。
var ajaxList = new AjaxList(settings);
ajaxList.refresh(); // etc.
// constructor of an instance
function MyObject( param1, param2 ) {
  this.param1 = param1;
  this.param2 = param2;
  this.param3 = 32;
  return this; // [optional]
}

// Public methods can be called by any instance.
// Instances share their prototype object.
// The this keyword always points to the current
// instance that calls the method.
MyObject.prototype.sum = function() {
  return this.param1 + this.param2 + this.param3;
}

// refresh should be a shared method, since it
// does the same thing on every instance
MyObject.prototype.refresh = function() {
  // do the refresh
  // ...
}