javascript 对象内的 If/else 条件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6329906/
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
If/else condition inside an Object
提问by John Cooper
function Validator(formIsValid) {
if(this.formIsValid) {
alert('Form is valid!');
}
else {
alert('Form is invalid...');
}
}
Validator.prototype = { // Notice the .prototype here, it's important!
formIsValid: true,
enforceTextFieldMinLength: function(field, minLength) {
if (!field.value || field.value.length < minLength) {
this.formIsValid = false;
}
},
enforceLabelHasText: function(label) {
if (!label.text) {
this.formIsValid = false;
}
}
}
//var val = new Validator();
The above is my Val.js. This is how i am using in my otherFile.js
以上是我的 Val.js。这就是我在 otherFile.js 中的使用方式
AddPatient.Firstname = FirstNameValue || Validator.enforceLabelHasText(FirstName);
I get an error saying cannot find function enforceLabelHasText in Object function Validator(formIsValid)
我收到一条错误消息 cannot find function enforceLabelHasText in Object function Validator(formIsValid)
回答by Lightness Races in Orbit
This is not valid syntax.
这不是有效的语法。
You've dumped an if/else
condition inside an object definition, like this:
您已经if/else
在对象定义中转储了一个条件,如下所示:
var myObj = { a, b, c, d,
if (true) {
alert('WTF!');
}
};
Procedural code like this must be inside a function.
像这样的过程代码必须在函数内部。
回答by Gabi Purcaru
You can't put expressions in an object definition. If you want code to be executed after an object instance is created, you should use:
您不能将表达式放在对象定义中。如果您希望在创建对象实例后执行代码,您应该使用:
function Validator() {
if(this.formIsValid) {
alert('Form is valid!');
}
else {
alert('Form is invalid...');
}
}
Validator.prototype = { // Notice the .prototype here, it's important!
formIsValid: true,
enforceTextFieldMinLength: function(field, minLength) {
if (!field.value || field.value.length < minLength) {
this.formIsValid = false;
}
},
enforceLabelHasText: function(label) {
if (!label.text) {
this.formIsValid = false;
}
}
}
var a = new Validator();
This is a dummy solution; you will want to add arguments to the Validator()
function, to initialize formIsValid
and the other values. I suggest you should read the MDC's description on prototypes.
这是一个虚拟的解决方案;您将需要向Validator()
函数添加参数、初始化formIsValid
和其他值。我建议你应该阅读MDC 对原型的描述。
EDIT: If you went with the prototype solution, you need to call val.enforceLabelHasText(FirstName)
, after making val
a global variable (either by omitting the var
or by using var window.val = new Validator()
).
编辑:如果您使用原型解决方案,则需要val.enforceLabelHasText(FirstName)
在创建val
全局变量(通过省略var
或使用var window.val = new Validator()
)后调用。
回答by Zach Willard
回答by alex
Validator
is an object literal and you can only assign properties there, not arbitrary code.
Validator
是一个对象字面量,你只能在那里分配属性,而不是任意代码。
You could assign a function which includes your code to a property.
您可以将包含您的代码的函数分配给属性。
回答by marko
Bind this to a variable in the beginning.
在开始时将其绑定到一个变量。
var that = this;
This keeps this changing and point to something else. And use firebug!
这会保持这种变化并指向其他东西。并使用萤火虫!