小型实用工具库是以奇异的方式行事吗?

时间:2014-06-24 18:48:46

标签: javascript

当我注意到我的实用程序库正在返回一个应用程序对象时,我试图在我的库中添加一个小实用程序功能。

它从完全其他文件返回一个对象。我的申请完全是私人的,在IIFE中。

您可以在此处查看该应用

www.arcmarks.com

中的控制台类型

localStorage.file_arcmarks_js

查看存储在localStorage中的应用程序文件。

var CArcmarks拥有一个看似自己全局的对象,现在由实用函数返回。

中的控制台类型

$A.extend({});

也看到它返回

Object { Name:CArcmarks ...

这是应用程序文件中的一个对象。

Extend是一个基本的实用程序函数,可以从另一个对象复制一个对象,如果您查看源

$A.extend

你可以看到它是一个非常简单的功能。

它是如何归还的。这很奇怪。

顺便说一句,这是在Chrome中。我正在检查其他浏览器......不是在FF中。也许是一些奇怪的Chrome漏洞?

我觉得我在暮光之城区域,是否有其他人看到这个或者有人吸毒我的摩卡。

此处的评论是CArcmarks和$ A.extend()的代码。

// *Pub is released to $A.
// does not extend through the prototype chain like similar underscore version
Pub.extend = function (obj) {

    // loop through elements beyond obj
    Pub.someIndex(nativeSlice.call(arguments, 1), function (val) {

        // extend it
        Pub.someKey(val, function (val_inner, key) {
            obj[key] = val_inner;
        });
    });
    return obj;
};

在另一个文件中:

/***************************************************************************************************
**CArcmarks
*/

    var CArcmarks = $A.Class.create({
        Name: 'CArcmarks',
        A: {
            toggle_page_lazy:           'toggle_page_lazy',
            toggle_page_active:         'toggle_page_active',
            toggle_tag_active:          'toggle_tag_active',
            toggle_tag_lazy:            'toggle_tag_lazy'
        },
        render: function (obj, el) {

            // element to render into is passed and a ns is extracted from the id
            // from this additional elements are extracted
            this.el = el;
            this.ns = el.id + '_';
            this.el_arcmarks = $A.el('#' + el.id + '_arcmarks');
            this.el_tags = $A.el('#' + el.id + '_tags');

            // holds data from the server to be inserted into template
            this.obj = obj;
            this.tags = {};
            this.composePane();
        },
        composePane: function () {

            // abstract out a page_string so the HTML is not tangled in the JavaScript
            var //favorite_string = '',
                tag_string = '',
                page_string = '',
                current_id,
                previous_id;
            _.each(this.obj, function (val) {
                previous_id = current_id;
                current_id = this.ns + val.tag;

                // a new tag has been found, create page and tag
                if ((previous_id !== current_id) || (previous_id === undefined)) {
                    this.tags[current_id] = '';
                    tag_string += this.composeTag(current_id);
                    page_string += this.composePage(current_id);

                    /*
                    if (previous_id !== undefined) {
                        favorite_string += '</div>';
                    }
                    favorite_string += this.composePage(current_id);
                    */
                }
                this.tags[current_id] += this.composeFavorite(val);
                // favorite_string += this.composeFavorite(val);
            }, this);

            // favorite_string = favorite_string + '</div>';
            if (!_.isEmpty(this.tags)) {
                this.el_arcmarks.innerHTML = page_string;
                // this.el_arcmarks.innerHTML = favorite_string;
                this.el_tags.innerHTML = tag_string;
                this.insertFavorites();
                this.initPane();
            }
        },
        insertFavorites: function () {
            $A.someChild(this.el_arcmarks, function (val) {
                if (val.id) {
                    val.innerHTML = this.tags[val.id.slice(0,-5)];
                }
            }, this);
        },
        composePage: function (current_id) {
            return _.template($('#item-page').html(), {
                foo: current_id + '_page'
            });
        },
        composeFavorite: function (val) {
            return _.template($('#item-arcmark').html(), {
                id:    this.ns + val.id,
                ida:   this.ns + val.id + 'a',
                idb:   this.ns + val.id + 'b',
                src:   val.favicon !== null ? val.favicon : 'http://arcmarks.com/arcmarks/images/image_null_50.png',
                href:  val.url,
                title: val.title
            });
        },
        composeTag: function (current_id) {
            var self = this;
            return _.template($('#item-tag').html(), {
                id:   current_id,
                foo:  current_id.slice(self.ns.length)
            });
        },
        initPane: function () {
            var self = this;
            _.each(this.tags, function (val, key) {
                $A.el('#' + key).addEventListener("click", function () {
                    self.flip(this);
                });
            }, this);
            this.flip($A.el('#' + $A.firstKey(this.tags)));
        },
        flip: function (tag_element) {
            var page_element = $A.el('#' + tag_element.id + '_page');

            // turn current page/tag on
            $A.toggleClass(page_element, this.A.toggle_page_active);
            $A.toggleClass(tag_element, this.A.toggle_tag_active);

            // turn off previous page/tag if it exists and it is not also the current
            if (this.prev_page_el && (tag_element.id !== this.prev_tag_el.id)) {
                $A.toggleClass(this.prev_page_el, this.A.toggle_page_lazy);
                $A.toggleClass(this.prev_tag_el, this.A.toggle_tag_lazy);
            }
            this.prev_page_el = page_element;
            this.prev_tag_el = tag_element;
        },
        insertFavorite: function (obj_fav) {
            var tag_id = this.ns + obj_fav.tag,
                div_el,
                html_string = this.composeFavorite(obj_fav),
                page_el = $A.el('#' + tag_id + '_page');
            div_el = $A.HTMLToElement(html_string);
            if (!page_el) {
                page_el = this.insertTagAndPage(tag_id);
            }
            $A.someChild(page_el, function (iter) {
                if (iter === null) {
                    page_el.appendChild(div_el);
                    return true;
                }
                if (obj_fav.title < iter.children[1].innerHTML) {
                    page_el.insertBefore(div_el, iter);
                    return true;
                }
                if (iter === page_el.lastChild) {
                    page_el.appendChild(div_el);
                    return true;
                }
            });
            this.flip($A.el('#' + tag_id));
            return div_el;
        },
        insertTagAndPage: function (tag) {
            var par_el,
                div_el,
                hold_tags_el,
                hold_arcmarks_el,
                self = this;
            hold_tags_el = this.el_tags;
            hold_arcmarks_el = this.el_arcmarks;
            par_el = $A.createElement('p');

            // update this
            par_el.innerHTML = tag.slice(this.ns.length);
            par_el.className = "single_tag";
            par_el.id = tag;

            // insert the tag(<p>)
            $A.someChild(hold_tags_el, function (iter) {
                if (iter === null) {
                    hold_tags_el.appendChild(par_el);
                    return true;
                }
                if (par_el.id < iter.id) {
                    hold_tags_el.insertBefore(par_el, iter);
                    return true;
                }
                if (iter === hold_tags_el.lastChild) {
                    hold_tags_el.appendChild(par_el);
                    return true;
                }
            });
            par_el.addEventListener("click", function () {
                self.flip(this);
            });
            div_el = $A.createElement('div');
            div_el.className = "bookmark_page";
            div_el.id = tag + "_page";
            div_el.style.display = "";
            $A.someChild(hold_arcmarks_el, function (iter) {
                if (iter === null) {
                    hold_arcmarks_el.appendChild(div_el);
                    return true;
                }
                if (div_el.id < iter.id) {
                    hold_arcmarks_el.insertBefore(div_el, iter);
                    return true;
                }
                if (iter === hold_arcmarks_el.lastChild) {
                    hold_arcmarks_el.appendChild(div_el);
                    return true;
                }
            });
            return div_el;
        }
    }, 'constructor');

0 个答案:

没有答案