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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 20:18:27  来源:igfitidea点击:

If/else condition inside an Object

javascript

提问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/elsecondition 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 formIsValidand 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 vala global variable (either by omitting the varor by using var window.val = new Validator()).

编辑:如果您使用原型解决方案,则需要val.enforceLabelHasText(FirstName)在创建val全局变量(通过省略var或使用var window.val = new Validator())后调用。

回答by Zach Willard

You caninsert logic into an object literal, using an iife. Like this;

可以使用iife将逻辑插入到对象文字中。像这样;

const testable = 1
const obj = {
  a: 'value1',
  b: (() => {
    if (testable === 1) {
      return 'testable was 1'
    } else {
      return 'testable was not 1'
    }
   })()
}

console.log(obj)

回答by alex

Validatoris 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!

这会保持这种变化并指向其他东西。并使用萤火虫!