调用 Javascript 构造函数中的方法并访问其变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2294190/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Calling a method in a Javascript Constructor and Accessing Its Variables
提问by alvincrespo
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! Thanks!
我正在尝试从我的 javascript 构造函数的构造函数中调用一个方法,这是否可能,如果是这样,我似乎无法让它工作,任何见解都会很棒!谢谢!
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”变量。
采纳答案by CMS
Yes, it is possible, when your constructor function executes, the thisvalue has already the [[Prototype]]internal property pointing to the ValidateFields.prototypeobject.
是的,有可能,当您的构造函数执行时,该this值已经具有[[Prototype]]指向该ValidateFields.prototype对象的内部属性。
Now, by looking at the your edit, the errArrayvariable is not available in the scope of the CreateErrorListmethod, since it is bound only to the scope of the constructor itself.
现在,通过查看您的编辑,该errArray变量在CreateErrorList方法的范围内不可用 ,因为它仅绑定到构造函数本身的范围。
If you need to keep this variable privateand only allow the CreateErrorListmethod 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 sharedand it will exist physically on all object instances of ValidateFields.
请注意,该方法由于绑定到this,因此不会被共享,并且它将物理存在于 的所有对象实例上ValidateFields。
Another option, if you don't mind to have the errArrayvariable, as a publicproperty of your object instances, you just have to assign it to the thisobject:
另一种选择,如果您不介意将errArray变量作为对象实例的公共属性,则只需将其分配给this对象:
//..
this.errArray = [];
//..
More info:
更多信息:
回答by alvincrespo
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.
希望这可以帮助将来可能有类似问题的任何人。
回答by Anurag
Are you creating an object of ValidateFieldssomewhere?
你是在ValidateFields某个地方创建一个对象吗?
Edit: You need to prepend thiswhenever referring to a function's public properties.
编辑:this每当引用函数的公共属性时,您都需要预先添加。
Updated the code here: http://jsbin.com/afiru/edit
在这里更新了代码:http: //jsbin.com/afiru/edit

