JavaScript idiom for limiting a string to a number of discrete values

In C# I might use an enumeration. In JavaScript, how can I limit a value to a set of discrete values idiomatically? We sometimes define a variable in a JS class 'Enumerations' along these lines: var Sex = { Male: 1, Female: 2 }; And then reference it just like a C# enumeration. There is no enumeration type in JavaScript. You could, however, wrap an object with a getter an

用于将字符串限制为多个离散值的JavaScript惯用法

在C#中,我可能会使用枚举。 在JavaScript中,我怎样才能将一个值限制为一组离散值的惯用方式? 我们有时会在这些行中定义一个JS类'枚举'中的变量: var Sex = { Male: 1, Female: 2 }; 然后像C#枚举一样引用它。 JavaScript中没有枚举类型。 然而,你可以用一个getter和setter方法来包装一个对象 var value = (function() { var val; return { 'setVal': function( v ) {

Converting enums to array of values (Putting all JSON values in an array)

I use this method Enums in JavaScript? to create enums in our code.. So var types = { "WHITE" : 0, "BLACK" : 1 } Now the issue is when I want create validations anywhere, I have to do this; model.validate("typesColumn", [ types.WHITE, types.BLACK ]); Now is there a way I can just simple convert the values in types to an array so that I don't have to list all of the values of the en

将枚举转换为值数组(将所有JSON值放入数组中)

我用这种方法在JavaScript中枚举? 在我们的代码中创建枚举 所以 var types = { "WHITE" : 0, "BLACK" : 1 } 现在的问题是我想在任何地方创建验证,我必须这样做; model.validate("typesColumn", [ types.WHITE, types.BLACK ]); 现在有什么办法可以简单地将类型中的值转换为数组,以便我不必列出枚举的所有值? model.validate("typesColumn", types.ValuesInArray]); 编辑 :我创建了一个非常简单的枚举库来生成

Enums with jQuery?

$("#bc [id$=_dropdownID]").change(function() { if (this.value == '2' || this.value == '3') { $("#bc .pnl").show(); } else { $("#bc .pnl").hide(); } I have the following code in jQuery. Is there any way I can replace the hard coded constants 2 and 3 in the above code with ac# enum? Does jQuery support enums and if so how can this be achieved? Any suggestions welc

枚举与jQuery?

$("#bc [id$=_dropdownID]").change(function() { if (this.value == '2' || this.value == '3') { $("#bc .pnl").show(); } else { $("#bc .pnl").hide(); } 我在jQuery中有以下代码。 有什么办法可以用ac#enum替换上面代码中的硬编码常量2和3吗? jQuery支持枚举吗?如果可以的话如何实现? 欢迎任何建议.... 你将不得不在JavaScript中复制枚举,如下所示: var myEnum = { On

Enums in Javascript with ES6

I'm rebuilding an old Java project in Javascript, and realized that there's no good way to do enums in JS. The best I can come up with is: const Colors = { RED: Symbol("red"), BLUE: Symbol("blue"), GREEN: Symbol("green") }; Object.freeze(Colors); The const keeps Colors from being reassigned, and freezing it prevents mutating the keys and values. I'm using Symbols so t

使用ES6在JavaScript中枚举

我正在用Javascript重建一个旧的Java项目,并意识到在JS中没有好的方法来枚举枚举。 我能想到的最好的是: const Colors = { RED: Symbol("red"), BLUE: Symbol("blue"), GREEN: Symbol("green") }; Object.freeze(Colors); const保持Colors不被重新分配,冻结它可以防止变更键和值。 我使用的是Symbols,所以Colors.RED不等于0 ,或者除了它本身以外的其他东西。 这个配方有没有问题? 有没有更好的办法?

How to shorten switch case block converting a number to a month name?

有没有一种方法可以在较少的行上编写此代码,但仍然易于阅读? var month = ''; switch(mm) { case '1': month = 'January'; break; case '2': month = 'February'; break; case '3': month = 'March'; break; case '4': month = 'April'; break; case '5': month = 'May'; break; case '6': month = 'June';

如何缩短将数字转换为月份名称的开关大小写块?

有没有一种方法可以在较少的行上编写此代码,但仍然易于阅读? var month = ''; switch(mm) { case '1': month = 'January'; break; case '2': month = 'February'; break; case '3': month = 'March'; break; case '4': month = 'April'; break; case '5': month = 'May'; break; case '6': month = 'June';

Sort JavaScript object by key

I need to sort JavaScript objects by key. Hence the following: { 'b' : 'asdsad', 'c' : 'masdas', 'a' : 'dsfdsfsdf' } Would become: { 'a' : 'dsfdsfsdf', 'b' : 'asdsad', 'c' : 'masdas' } The other answers to this question are outdated, never matched implementation reality, and have officially become incorrect now that the ES6/ES2015 spec has been published. See the section on property itera

按键排序JavaScript对象

我需要按键排序JavaScript对象。 因此如下: { 'b' : 'asdsad', 'c' : 'masdas', 'a' : 'dsfdsfsdf' } 会成为: { 'a' : 'dsfdsfsdf', 'b' : 'asdsad', 'c' : 'masdas' } 对于这个问题的其他答案已经过时,从未匹配实现的实际情况,并且现在ES6 / ES2015规范已经发布,并且已经正式变得不正确。 Axel Rauschmayer在Exploring ES6中查看属性迭代顺序部分: 迭代属性键的所有方法都按照相同的顺序进行: 首先是所有数

How can I wrap text around stacked elements (ie elements with negative margins)?

I'm hope someone can help me with this. I would like to wrap text around multiple stacked floated elements, however when I am adding a negative margin to the second element the text does not play ball (see below)... Does anyone have a solution that can help me with this? Thanks in advance! What I've done so far here: <style> .elements { float:left; padding:10px;

我怎样才能围绕堆叠元素(即具有负边距的元素)换行?

我希望有人能帮助我。 我想围绕多个堆叠浮动元素进行文本打包,但是当我为第二个元素添加负边距时,文本不会弹出球体(请参阅下文)... 有没有人有解决方案,可以帮助我呢? 提前致谢! 到目前为止我所做的是: <style> .elements { float:left; padding:10px; width:50%; background:#039; color:#fff; font-family:Arial, Helvetica, sans-serif; color:#fff; padding:50px;

Sorting and filtering on Date

I have a date column and need to be able to both sort and filter on it. The data comes in as strings like 2010-12-23 and can pre-processed as needed. It should be shown as 23.12.2010 . Some internationalization will come later. I wonder what's the proper internal representation: a string like "23.12.2010" is bad for sorting (it could be done by sorting on function result, but

在日期排序和筛选

我有一个日期列,需要能够对它进行排序和过滤。 数据以2010-12-23字符串的形式出现,并可根据需要进行预处理。 它应该显示为23.12.2010 。 一些国际化会晚一些。 我想知道什么是适当的内部表示: 像"23.12.2010"这样的字符串不利于排序(可以通过对函数结果进行排序来完成,但它会很慢) 像"2010-12-23"这样的字符串排序正确,可以很容易地格式化,但过滤23.12不起作用(可以完成,但速度会很慢)

Textarea placeholder isn't shown in IE 11 being rendered using React

I have following HTML: <textarea placeholder="Usual textarea placeholder"></textarea> <div id="react-container"></div> ... and following JS: ReactDOM.render( <textarea placeholder="React textarea placeholder"/>, document.getElementById('react-container') ); So, I am trying to show the same things using React and using usual HTML. However, in IE 11 at le

在使用React渲染IE 11时,Textarea占位符未显示

我有以下HTML: <textarea placeholder="Usual textarea placeholder"></textarea> <div id="react-container"></div> ...和以下JS: ReactDOM.render( <textarea placeholder="React textarea placeholder"/>, document.getElementById('react-container') ); 所以,我试图使用React和使用通常的HTML来展示相同的东西。 但是,至少在IE 11中,使用React呈现的textarea的行为如下所

Unique Id without submission of form in ajax php

I have a form there are 5 fields where I want to automatic generate roll number in 5th field after filling up 4 fields(Name,Phone,Course,Batch) without submitting form.but after filling up 4 field no value comes in 5th field(roll).Below is my code index.php <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" <head> <title>Untitled Document</title> <script>

独一无二的标识,无需在Ajax中提交表单

我有一个表格,其中有5个字段,我想在填写4个字段(名称,电话,课程,批处理)之后在第5个字段中自动生成滚动编号而无需提交表格。但是在填充4个字段后,第5个字段中没有值roll).Below是我的代码 的index.php <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" <head> <title>Untitled Document</title> <script> function my_validate_func() { if ($('#name').val(