简体   繁体   English

在javascript中控制相关对象的正确方法是什么?

[英]What is the proper way to control related objects in javascript?

I'm new to object oriented programming and am slowly learning how to apply it to javascript. 我是面向对象编程的新手,正在慢慢学习如何将它应用于javascript。 So please bear with me. 所以请耐心等待。 :) :)

I have two basic objects: 我有两个基本对象:

  1. "record" which contains methods for editing a single record from a recordset. “record”,包含从记录集编辑单个记录的方法。 (create, save, load, etc.) (创建,保存,加载等)

  2. "recordList" which contains methods for outputting a paginated list of record titles. “recordList”,其中包含用于输出分页记录标题列表的方法。

I would like for these objects to be able to work together. 我希望这些对象能够一起工作。 For example, if record.save() is called, recordList.refresh() is also called, so that the paginated list reflects the updated data. 例如,如果调用record.save(),则还会调用recordList.refresh(),以便分页列表反映更新的数据。

To accomplish this, I have created a third object "control" which contains instances of both "record" and "recordList". 为了实现这一点,我创建了第三个对象“control”,它包含“record”和“recordList”的实例。 I am using "control" in the following fashion: 我以下列方式使用“控制”:

control = {}

    control.record = object.create("record");
    control.recordList = object.create("recordList");

    control.save = function() {

        this.record.save();
        this.recordList.refresh();

    };

This works. 这有效。 But I am wondering, is it proper? 但我想知道,这是正确的吗? (I want to be sure I am not violating any of the rules of OO design in doing this.) Is there a better way? (我想确保在执行此操作时我没有违反OO设计的任何规则。)有更好的方法吗?

Thanks in advance for your help. 在此先感谢您的帮助。

Your solution is fine. 你的解决方案很好。 Two minor suggestions for improvement 两个小改进建议

  1. Use a more specific name than control (even 'recordControl' is ok). 使用比控件更具体的名称(即使'recordControl'没问题)。 You may end up with lots of controls for different feature sets. 您最终可能会针对不同的功能集进行大量控制。

  2. Use an object literal to create the entire object. 使用对象文字来创建整个对象。 Keeps your code tidier and more readable (and saves a few bytes) 保持您的代码更整洁,更可读(并节省几个字节)

(apologies for lack of spacing - editor not doing what I want it to do!) (道歉是因为缺乏间距 - 编辑没有做我想做的事!)

    recordControl = {
        record : object.create("record"),
        recordList : object.create("recordList"),
        save : function() {
            this.record.save();
            this.recordList.refresh();
        }
    }

Speaking from an OOP perspective, I don't think a record would save itself. 从OOP的角度来看,我不认为记录可以自救。 A record in a database is simply data, and the database itself is what does things with that data, whether it's saving or loading or etc. That being said I'd make record be simply an object that holds data and would create a recordset object for interacting with the data. 数据库中的记录只是数据,而数据库本身就是对数据进行处理的事情,无论是保存还是加载等等。据说我记录的只是一个保存数据的对象,并会创建一个记录集对象用于与数据交互。 Within that recordset object you could put your recordList and update it accordingly. 在该记录集对象中,您可以放置​​recordList并相应地更新它。 Something like: 就像是:

var recordset = function() {
    var me = this;
    var currentRecord = object.create("record");
    var recordList = object.create("recordList");
    me.save = function() {
            //Insert record.save code here
            recordList.refresh();
    };
};

Something to note about that code. 有关该代码的注意事项。 In that setup currentRecord and recordList can't be accessed from outside the function and therefore you have encapsulation, one of the hallmarks of OOP. 在那个设置中,currentRecord和recordList不能从函数外部访问,因此你有封装,这是OOP的标志之一。 This is because the recordset function is a closure that "closes over" all variables within, meaning that every function within has access to the variables within the scope of recordset. 这是因为记录集函数是一个闭包“封闭”其中的所有变量,这意味着其中的每个函数都可以访问记录集范围内的变量。

You could let the outside world get access through get or set functions: 您可以通过获取或设置功能让外部世界获得访问权限:

    me.getRecordList = function() {
        return recordList.getArray(); //Not generally a good idea to simply return your internal object
    };

If it's one thing I've learned over time, it is that following any paradigm to the letter will result in more frustration and difficulty than taking the concept as far as you can go and using common sense to dictate your deviations. 如果这是我随着时间的推移而学到的一件事,那么遵循任何范式的信件都会导致更多的挫折和困难,而不是采取这个概念,尽可能地使用常识来指示你的偏差。

That said, your solution will work fine and it's normal to create a container class for multiple objects of varying types that are coupled. 也就是说,您的解决方案将正常工作,为多个不同类型的对象创建容器类是正常的。 If you want a different way to handle it, check out Client Event Pooling . 如果您想要一种不同的方式来处理它,请查看客户端事件池 The only thing that I can say about what you've done is to be sure you're using object.create the way it was intended. 关于你所做的事情,我唯一能说的就是确保你正在使用object.create按照预期的方式。

Using this method you can create an event, which when triggered will perform a series of other commands that are associated with your event. 使用此方法,您可以创建一个事件,该事件在触发时将执行与您的事件关联的一系列其他命令。 I have used this with great success in all sorts of applications, from the intended event hooking to simplifying inline javascript injections after a postback. 我已经在各种应用程序中取得了巨大的成功,从预期的事件挂钩到在回发后简化内联javascript注入。

Good luck. 祝好运。

why don't you provide your recordList into record? 你为什么不把你的记录列表记录下去?

var record = object.create("record");
record.recordList = object.create('recordList');
record.save = function(){
    /*
        do something 
    */
    this.recordList.refresh();
}

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

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