JavaScript代码解释
有人可以向我解释一下这段代码的作用吗?
dojo[(show ? "remove" : "add") + "Class"](this.listNode, "tweetviewHidden");
这是该函数值所属的函数:
// Provide the class
dojo.provide("tweetview._ViewMixin");
// Declare the class
dojo.declare("tweetview._ViewMixin", null, {
// Returns this pane's list
getListNode: function() {
return this.getElements("tweetviewList",this.domNode)[0];
},
// Updates the list widget's state
showListNode: function(show) {
dojo[(show ? "remove" : "add") + "Class"](this.listNode, "tweetviewHidden");
},
// Pushes data into a template - primitive
substitute: function(template,obj) {
return template.replace(/\$\{([^\s\:\}]+)(?:\:([^\s\:\}]+))?\}/g, function(match,key){
return obj[key];
});
},
// Get elements by CSS class name
getElements: function(cssClass,rootNode) {
return (rootNode || dojo.body()).getElementsByClassName(cssClass);
}
});
来源:http://dojotoolkit .org/documentation/tutorials/1.6/mobile/tweetview/starting_tweetview
Can someone explain to me what this code does?
dojo[(show ? "remove" : "add") + "Class"](this.listNode, "tweetviewHidden");
Here's the function in which this function value belongs to:
// Provide the class
dojo.provide("tweetview._ViewMixin");
// Declare the class
dojo.declare("tweetview._ViewMixin", null, {
// Returns this pane's list
getListNode: function() {
return this.getElements("tweetviewList",this.domNode)[0];
},
// Updates the list widget's state
showListNode: function(show) {
dojo[(show ? "remove" : "add") + "Class"](this.listNode, "tweetviewHidden");
},
// Pushes data into a template - primitive
substitute: function(template,obj) {
return template.replace(/\$\{([^\s\:\}]+)(?:\:([^\s\:\}]+))?\}/g, function(match,key){
return obj[key];
});
},
// Get elements by CSS class name
getElements: function(cssClass,rootNode) {
return (rootNode || dojo.body()).getElementsByClassName(cssClass);
}
});
Source: http://dojotoolkit.org/documentation/tutorials/1.6/mobile/tweetview/starting_tweetview
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
很简单,如果 show 为 true,它将调用 dojo.removeClass(this.listNode, "tweetviewHidden"); 如果为 false,它将调用 dojo.addClass(this.listNode, “tweetviewHidden”);。
本质上它是一个切换功能。
[ ] 括号打开一个对象以通过键访问值。就像 var bla={"foo":"bar"}; bla["foo"];.现在,由于它是dojo,该值是一个函数,它将被执行
quite simple, if show is true, it will call
dojo.removeClass(this.listNode, "tweetviewHidden");
and if its false, it will calldojo.addClass(this.listNode, "tweetviewHidden");
.Essentially its a toggling function.
the [ ] brackets open up an object to access the value by key. just like
var bla={"foo":"bar"}; bla["foo"];
. Now, since its dojo, the value is a function, which will be executed稍微详细一点,代码做了这样的事情:
我想
dojo
充当可以通过[]
按名称查找的函数的容器。Slightly more verbosely, the code does something like this:
I suppose
dojo
acts as a container for functions that can be looked up by name via[]
.它使用
[]
括号和?:
三元运算符来执行如下操作:It uses the
[]
brackets and the?:
ternary operator, to do something like this:它通过“show”变量“切换”类。也就是说,如果“show”为真,则该类将从节点中删除,否则将添加到节点中。
考虑使用以下快捷方式:
It "toggles" the class via the "show" variable. That is, if "show" is truthy, then the class will be removed from the node, otherwise it will be added to the node.
Consider using the following shortcut: