简体   繁体   English

如何创建一个包含特殊功能的格式良好的全局javascript对象

[英]How to create a well formed global javascript object containing special functions

I am creating a small project that heavily relies on JavaScript. 我正在创建一个严重依赖JavaScript的小项目。 I come from php/mysql and now stepping into node.js/javascript/mongodb, and I hve to say it's quite a mindswitch. 我来自php / mysql,现在踩到node.js / javascript / mongodb,我想说这是一个很好的思想开关。

I want to create a simple object that has some special function that I can use in the page. 我想创建一个简单的对象,它具有一些我可以在页面中使用的特殊功能。 I have been looking at some tutorial, and looking at the libraries such as jquery and backbone, but I need some final advice on my decision. 我一直在看一些教程,并查看jquery和backbone等库,但我需要对我的决定提出一些最终建议。

I only need some small functions, and no cross-browser support, that's why I don't choose something like backbone. 我只需要一些小功能,没有跨浏览器支持,这就是为什么我不选择像骨干这样的东西。 Maybe ill change to that later when I have a better crasp on JavaScript programming. 当我在JavaScript编程上有更好的崩溃时,可能会对此有所改变。

What is confusing me is whether to use the new , or maybe wrapping the code into a self-invoking function. 令我困惑的是,是否使用new ,或者可能将代码包装到自调用函数中。

I see jquery creates an object inside the window and than exposes that, but I have no idea how that works. 我看到jquery在window内创建了一个对象而不是公开它,但我不知道它是如何工作的。

Enough intro, now to the point. 足够的介绍,现在到了这一步。 I have created something like this: 我创建了这样的东西:

var $s = Object.create({

    page: Object.create({

        title: 'pagetitle',
        html: '',
        data: {},

        render: function(){
            // Basic render function
        }

    }),

    socket: Object.create({
        // My websocket connection
    }),

    store: function(key, value) {
        localStorage.setItem(key, JSON.stringify(value));
    },

    retrieve: function(key) {
        var value = localStorage.getItem(key);
        return value && JSON.parse(value);
    },

    slugify: function(slug){
        return slug.replace(/[^a-zA-Z 0-9-]+/g,'').toLowerCase().replace(/ /g,'-');
    }

});

This are just a few random functions I put in. 这只是我输入的几个随机函数。

I haven't tested this yet, it is a draft, I want to know if this is any good. 我还没有测试过这个,它是一个草稿,我想知道这是否有用。

Now I was thinking i can do some stuff like this: 现在我想我可以做这样的事情:

$s.page.html = 'somehtml';
$s.page.render();

// Maybe
$s.store( $s.page.title, $s.page.html );

I do use jQuery and jQuery templating, so something like this could be possible: 我使用jQuery和jQuery模板,所以这样的事情是可能的:

$.tmpl( $s.page.html, $s.page.data ).appendTo( "#content" );

Nothing fancy is needed here. 这里没有什么花哨的东西。 You can create a global javascript object with a method like this: 您可以使用如下方法创建全局javascript对象:

var myGlobalObject = {};
myGlobalObject.testFunction = function() {
    // put your code here
};

You can then call that like this: 然后您可以这样调用:

myGlobalObject.testFunction();

One slightly more flexible design pattern you will often seen used is this: 您将经常看到的一种稍微灵活的设计模式是:

var myGlobalObject = myGlobalObject || {};

myGlobalObject.testFunction = function() {
    // put your code here
};

This is used when there might be lots of different pieces of code contributing to myGlobalObject and they all want to make sure that it's properly declared before adding properties to it. 当可能有许多不同的代码片段对myGlobalObject ,它们会被使用,并且它们都希望确保在向其添加属性之前正确声明它。 This way of doing it, creates it if it doesn't already exist and if it does already exist, leaves the methods and properties on it that might already be there. 这种方式,如果它尚不存在则创建它,如果它已经存在,则在其上留下可能已存在的方法和属性。 This allows multiple modules to each contribute initialization to myGlobalObject without regards for the order they load. 这允许多个模块分别为myGlobalObject提供初始化,而不考虑它们加载的顺序。

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

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