将模块模式与Prototype相结合(Combining Module pattern with Prototype)
我试图通过使用模块模式封装一些方法。 我也试图通过使用原型来避免新对象创建的新函数定义。 问题是我必须实现一个构造函数,我无法弄清楚如何。 我的代码:
// constructor function Anagram(original) { this.original = original.toLowerCase() this.sortedOriginal = this.sort(this.original) } // prototype as a module Anagram.prototype = (function() { function isAnagram(word) { word = word.toLowerCase() return this.original != word && sameLetters(word) } function sameLetters(word) { return this.sortedOriginal === sort(word) } function sort(str) { return str.split('').sort().join('') } return { match: function(words) { return words.filter(isAnagram, this) } } }()) module.exports = Anagram
运行
Anagram.new('ant').match(['tan', 'stand', 'at'])
失败了
TypeError: Object #<Object> has no method 'sort'
我理解为什么,
sort
没有在构造函数中定义。 我该如何解决?I am trying to encapsulate some methods by using module pattern. I am also trying to avoid new function definitions on new object creation by using a prototype. The problem is I have to implement a constructor and I couldn't figure how. My code:
// constructor function Anagram(original) { this.original = original.toLowerCase() this.sortedOriginal = this.sort(this.original) } // prototype as a module Anagram.prototype = (function() { function isAnagram(word) { word = word.toLowerCase() return this.original != word && sameLetters(word) } function sameLetters(word) { return this.sortedOriginal === sort(word) } function sort(str) { return str.split('').sort().join('') } return { match: function(words) { return words.filter(isAnagram, this) } } }()) module.exports = Anagram
Running
Anagram.new('ant').match(['tan', 'stand', 'at'])
Fails with
TypeError: Object #<Object> has no method 'sort'
And I understand why,
sort
is not defined in the constructor. How can I fix it?
原文:https://stackoverflow.com/questions/21191599
更新时间:2023-05-21 21:05
最满意答案
首先选择表格,然后查找子行:
jQuery("table").find("tr:odd").css("background-color","#f5f5f5");
http://jsfiddle.net/mblase75/xgQ8Q/
Vega的答案使用相同的方法,使用更少的字符。
Start by selecting the tables, then finding the child rows:
jQuery("table").find("tr:odd").css("background-color","#f5f5f5");
http://jsfiddle.net/mblase75/xgQ8Q/
Vega's answer uses the same approach with fewer characters.
相关问答
更多-
表行奇数/偶数重置(Table Row Odd/Even Reset)[2023-01-29]
首先选择表格,然后查找子行: jQuery("table").find("tr:odd").css("background-color","#f5f5f5"); http://jsfiddle.net/mblase75/xgQ8Q/ Vega的答案使用相同的方法,使用更少的字符。 Start by selecting the tables, then finding the child rows: jQuery("table").find("tr:odd").css("background-color", ... -
不是我所知道的,但是(编辑时)你可以使用Mx hl-line-mode清楚地看到当前行突出显示。 Not that I know of, but (when editing) you can use M-x hl-line-mode to clearly see the current row highlighted.
-
将css规则应用于偶数编号的表行 - 在标题行之后重置(Apply a css rule to even numbered table rows - reset after header row)[2023-05-22]
您可以使用~ with :nth-of-type选择器: tr.subhead ~ tr:nth-of-type(even) { /* your style*/ } .subhead ~ tr将范围从子.subhead ~ tr旁边的所有tr开始。 You can use ~ along with :nth-of-type selector: tr.subhead ~ tr:nth-of-type(even) { /* your style*/ } .subhead ~ tr will scope al ... -
这是基于Grails附带的脚手架模板。 您可以使用install-templates命令( 请参阅文档 ),然后编辑相应的模板以更改/删除它。 This is based on the scaffolding templates that get included with Grails. You can use the install-templates command (see docs) and then edit the corresponding template to change/remove ...
-
... ... -
当然,只需独立选择每个table的行: $(".myTable").find('.myRow:odd').addClass('myAltRowClass'); JS Fiddle演示 。 或者,最好只使用CSS: table.myTable tr:nth-child(even) { background: #DDDDDC; } JS Fiddle演示 。 请注意, even在CSS :nth-child()示例中,我也切换到了,这是因为JavaScript是基于零的,而CSS是基于一个的。 或者 ...
-
CSS中最接近动态值的是计数器,但计数器不能与选择器一起使用。 选择器大多是静态的,动态伪类除外。 你不能使用CSS来设置一个任意的值,这会影响选择器与元素的匹配。 正如评论中所建议的那样,使用多个tbody元素可以更好地表示您的表格结构,每个部分都有一个元素。 这也会允许你现有的:nth-child()选择器匹配正确的行元素: