JavaScript代码解释

发布于 2024-11-17 17:04:58 字数 1726 浏览 2 评论 0原文

有人可以向我解释一下这段代码的作用吗?

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

世界如花海般美丽 2024-11-24 17:04:58

很简单,如果 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 call dojo.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

妳是的陽光 2024-11-24 17:04:58

稍微详细一点,代码做了这样的事情:

if (show) { f = dojo["removeClass"]; }
else      { f = dojo["addClass"];    }

f(this.listNode, "tweetviewHidden");

我想 dojo 充当可以通过 [] 按名称查找的函数的容器。

Slightly more verbosely, the code does something like this:

if (show) { f = dojo["removeClass"]; }
else      { f = dojo["addClass"];    }

f(this.listNode, "tweetviewHidden");

I suppose dojo acts as a container for functions that can be looked up by name via [].

复古式 2024-11-24 17:04:58

它使用 [] 括号和 ?: 三元运算符来执行如下操作:

if(show){
    dojo.removeClass(this.listNode, "tweetviewHidden");
}else{
    dojo.addClass(this.listNode, "tweetviewHidden");
}

It uses the [] brackets and the ?: ternary operator, to do something like this:

if(show){
    dojo.removeClass(this.listNode, "tweetviewHidden");
}else{
    dojo.addClass(this.listNode, "tweetviewHidden");
}
陈年往事 2024-11-24 17:04:58

它通过“show”变量“切换”类。也就是说,如果“show”为真,则该类将从节点中删除,否则将添加到节点中。

考虑使用以下快捷方式:

dojo.toggleClass(this.listNode, "tweetviewHidden", !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:

dojo.toggleClass(this.listNode, "tweetviewHidden", !show);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文