简体   繁体   English

这是什么样的模式?

[英]What kind of pattern is this?

I've learnt development by looking at other people's codes, so I'm not very good with terminologies. 我是通过查看其他人的代码来学习开发的,所以我对术语不是很好。 Lately I've been writting my JS/Jquery this way: 最近,我一直以这种方式编写我的JS / Jquery:

$(document).ready(function() {
    testingFunc.init();
});

var testingFunc = {
    $object: $('#object'),
    init: function() {
        var _that = this;
        console.log($object);
    }
}

Can someone please tell me if this a pattern of some sort? 有人可以告诉我这是否是某种模式吗? Or can someone please tell me how to describe the code I've done above? 或者有人可以告诉我如何描述我上面完成的代码?

This particular style represented in your code is an " object literal " pattern. 您的代码中表示的这种特定样式是“ 对象文字 ”模式。 It differs only slightly from a " module " pattern when you find yourself not requiring specific properties or methods to be private. 当您发现自己不需要特定的属性或方法为私有时,它与“ 模块 ”模式仅略有不同。

Before getting into a trap of terminologies, you may want to understand (in principle) what Javascript patterns are, and then identify those which may be architecturally best-fit for your project. 在陷入术语陷阱之前,您可能想要(原则上)理解什么是Javascript模式,然后确定在架构上最适合您的项目的Javascript模式。

You may get an in-depth understanding from this mini-book from Addy Osmani: 您可以从Addy Osmani的这本迷你书中获得深入的了解:

http://addyosmani.com/resources/essentialjsdesignpatterns/book/ http://addyosmani.com/resources/essentialjsdesignpatterns/book/

And a high-level article from him: 还有他的一篇高级文章:

http://addyosmani.com/largescalejavascript/ http://addyosmani.com/largescalejavascript/

Perhaps you can name it the Object Literal pattern like used by Rebecca Murphey in her article. 也许您可以将其命名为Object Literal模式,就像Rebecca Murphey在她的文章中使用的那样。 However I do not think that it's widely adopted as an official name for this kind of code structure, but it seems appropriate. 但是,我不认为这种代码结构已被广泛采用为正式名称,但这似乎是适当的。

The first part is using a jQuery selector with the listener "ready". 第一部分是将jQuery选择器与侦听器“就绪”一起使用。 What this means is that the callback function attached to the selector and listener will run once the document (in this case the browser window) is ready (in web terms, this means when the page finishes loading). 这意味着附加到选择器和侦听器的回调函数将在文档(在本例中为浏览器窗口)准备就绪(以Web术语表示,这意味着页面完成加载)后运行。

The second part of your code is following a standard called object literal, which is a JavaScript methodology that follows the principles of key->value 代码的第二部分遵循称为对象文字的标准,这是一种遵循key-> value原理的JavaScript方法

I guess you are wondering about the ready function. 我想您想知道ready功能。 In order to understand how it works, you have to know that when you load an HTML page into you browser, the HTML structure is turned into a javascript tree called "DOM" (Document Object Model). 为了理解它的工作原理,您必须知道在将HTML页面加载到浏览器时,HTML结构变成了一个名为“ DOM”(文档对象模型)的JavaScript树。 In your sample, the DOM is referenced through the variable named document . 在您的示例中,通过名为document的变量引用了DOM。 To populate this tree, each markup has to be initialized as a javascript object. 要填充此树,必须将每个标记初始化为javascript对象。 Once this job is done, the "ready" event is raised, invoking every function which is bound to it. 完成此工作后,将引发“就绪”事件,并调用与其绑定的每个函数。 To summarize : 总结一下:

$(document).ready(function () { testingFunc.init(); });
// translation : Once the DOM has been initialized, call "init".

Regarding your code, $('#object') attempts to query the DOM tree to find a node with an id set to "object" (eg <div id="object"> ). 关于您的代码, $('#object')尝试查询DOM树以查找ID设置为“ object”的节点(例如<div id="object"> )。 However, the document is probably not yet fully initialized. 但是,该文档可能尚未完全初始化。 As a result, this query might fail. 结果,此查询可能会失败。 To avoid this risk you should rather do this : 为了避免这种风险,您应该这样做:

var testingFunc = {
    $object: null,
    init: function() {
        this.$object = $('#object');
        console.log(this.$object);
    }
}

You can think of the DOM as a folder structure, where each folder and file is an HTML markup. 您可以将DOM视为文件夹结构,其中每个文件夹和文件都是HTML标记。 jQuery browses the DOM tree the same way that you browse your files explorer. jQuery浏览DOM树的方式与浏览文件浏览器的方式相同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM