简体   繁体   English

OO(面向对象)javascript

[英]OO(object-oriented) javascript

I've a fair amount of experience with JavaScript, and for this new project I'm on (cms for blog with notions of profitability) I thought I'd step it up and write the JavaScript in an MVC fashion. 我在JavaScript方面有相当丰富的经验,对于这个新项目(关于盈利概念的博客,请访问CMS),我想我应该加倍努力,并以MVC方式编写JavaScript。 I've been using a bit of backbone and underscore, but it isn't clicking mentally. 我一直在使用一些骨干和下划线,但它并不是在精神上点击。 any way, I've written a bit of code to handle some events/effects but it just doesn't work. 无论如何,我已经写了一些代码来处理一些事件/效果,但这根本行不通。 If anyone could sort me out I'd really appreciate it. 如果有人能解决我,我将非常感激。

// Semi Perfect grade 0 JS - Golden age
//partial View Objects | Events
var pshare_dock = {
    actor: $("#share_dock"),
    drag: function () {
        this.actor.draggable();
    }
}
pshare_dock.expand = function () {
    this.actor.dblclick(function () {
        $(this).toggleClass("share_close");
    });
}
var pmenu = {
    hover: function () {
        $("ul.drop li.drop").hover(function () {
            $(this).find('ul').fadeIn(1);
        }, function () {
            $(this).find('ul').hide();
        })
    },
    navigate: function () {
        $("a.ajx").click(function (e) {
            var link;
            var container = $("#content_pane");
            e.preventDefault();
            link = $(this).attr("href") + "#content_pane";
            container.load(link);
        })
    }
}
var pcontent_pane = {}
var ppost = {}
var pdatabase_entry = {}
//Views
var Homepage = function () {
        this.share_dock = function () {
            new pshare_dock();
        }
        this.menu = function () {
            new pmenu();
        }
        this.content_pane = function () {
            new pcontent_pane();
        }
        this.posts = function () {
            new ppost();
        }
    }
    //Controller
var GoldenAgeRouter = Backbone.Router.extend({
    routes: {
        "!/": "defaultRoute",
        "*actions": "defaultRoute"
    },
    defaultRoute: function (actions) {
        var homeView = function () {
                new Homepage();
            }
    }
})
$(document).ready(function () {
    var Golden_age = function () {
            new Homepage();
        }
})

the question is essentially what all is wrong with this? 问题本质上是这到底有什么问题?

You're wrapping your instantiations in an anonymous function but not invoking them: 您将实例化包装在匿名函数中,但未调用它们:

var Golden_age = new Homepage(); // Invoked.

var Golden_age = function(){ new Homepage(); } // Stored function, not invoked.

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

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