简体   繁体   English

在Javascript构造函数中调用方法并访问其变量

[英]Calling a method in a Javascript Constructor and Accessing Its Variables

I am trying to call a method from the constructor of my javascript constructor, is this possible and if so, I can't seem to get it working, any insight would be great! 我试图从我的javascript构造函数的构造函数调用方法,这是否可能,如果是这样,我似乎无法使它正常工作,那么任何见识都将很棒! Thanks! 谢谢!

function ValidateFields(pFormID){
    var aForm = document.getElementById(pFormID);
    this.errArray = new Array();//error tracker
    this.CreateErrorList();
}
/*
 * CreateErrorList()
 * Creates a list of errors:
 *   <ul id="form-errors">
 *    <li>
 *     You must provide an email.
 *    </li>
 *   </ul>
 * returns nothing
 */
 ValidateFields.prototype.CreateErrorList = function(formstatid){
     console.log("Create Error List");
 }

I got it to work with what is above, but I can't seem to access the 'errArray' variable in CreateErrorList function. 我可以使用上面的内容,但是我似乎无法访问CreateErrorList函数中的'errArray'变量。

Yes, it is possible, when your constructor function executes, the this value has already the [[Prototype]] internal property pointing to the ValidateFields.prototype object. 是的,执行构造函数时, this值可能已经具有指向ValidateFields.prototype对象的[[Prototype]]内部属性。

Now, by looking at the your edit, the errArray variable is not available in the scope of the CreateErrorList method, since it is bound only to the scope of the constructor itself. 现在,通过查看您的编辑, errArray变量在CreateErrorList方法的范围内不可用,因为它仅绑定到构造函数本身的范围。

If you need to keep this variable private and only allow the CreateErrorList method to access it, you can define it as a privileged method , within the constructor: 如果需要将此变量设置为私有,并且仅允许CreateErrorList方法访问它,则可以在构造函数中将其定义为特权方法

function ValidateFields(pFormID){
  var aForm = document.getElementById(pFormID);
  var errArray = [];

  this.CreateErrorList = function (formstatid){
    // errArray is available here
  };
  //...
  this.CreateErrorList();
}

Note that the method, since it's bound to this , will not be shared and it will exist physically on all object instances of ValidateFields . 请注意,由于该方法已绑定this ,因此不会共享,并且该方法将物理存在于ValidateFields所有对象实例上。

Another option, if you don't mind to have the errArray variable, as a public property of your object instances, you just have to assign it to the this object: 另一个选择是,如果您不介意将errArray变量作为对象实例的公共属性,则只需将其分配给this对象:

//..
this.errArray = [];
//..

More info: 更多信息:

Solution: 解:

function ValidateFields(pFormID){
    console.log("ValidateFields Instantiated");
    var aForm = document.getElementById(pFormID);
    this.errArray = new Array();//error tracker
    this.CreateErrorList(); //calling a constructors method
}

ValidateFields.prototype.CreateErrorList = function(){
   console.log("Create Error List");
   console.log(this.errArray); //this is how to access the constructors variable
}

Hope this helps anyone who might have a question like this in the future. 希望这对以后可能会遇到此类问题的人有所帮助。

Are you creating an object of ValidateFields somewhere? 您是否在某处创建ValidateFields对象?

Edit : You need to prepend this whenever referring to a function's public properties. 编辑 :每当引用函数的公共属性时,都需要在this前缀之前。

Updated the code here: http://jsbin.com/afiru/edit 在此处更新了代码: http : //jsbin.com/afiru/edit

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

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