简体   繁体   English

JavaScript中共享功能的OOP模式(例如记录器)

[英]OOP Pattern for Shared Functionality (e.g. Logger) in JavaScript

I need recommendations for how to program common functionality into my JavaScript application. 我需要有关如何将常用功能编程到我的JavaScript应用程序中的建议。 For example, there are these classes: Main, Logger, App and Template. 例如,有以下类别:Main,Logger,App和Template。

Main creates an instance of App and Logger, then App creates an instance of Template. Main创建App和Logger的实例,然后App创建Template的实例。 Main passes a reference of itself into App's init, and App passes it's reference of Main to Template's init. Main将其自身的引用传递给App的init,而App将其Main的引用传递给Template的init。 Now, for right or wrong, Template has direct reference to the Logger instance within Main. 现在,无论是对还是错,Template都可以直接引用Main中的Logger实例。 This is the simplest, but it doesn't seem like proper OOP as any change to logger would require rep-programming every class that invokes the reference. 这是最简单的方法,但是它似乎不是适当的OOP,因为对记录器的任何更改都需要对调用该引用的每个类进行rep-program。

Alternatively, Template could receive only a reference to App, that has a method to invoke it's reference to Main, which has a method to invoke it's instance of Logger. 另外,Template可能只收到对App的引用,该引用具有一种方法可以调用它对Main的引用,Main具有可以调用Logger实例的方法。 This seems safer in terms to keeping everyone at bay, but it also means Main will be full of methods simply to access Logger and other classes it manages. 从每个人的角度来看,这似乎更安全,但这也意味着Main将会拥有很多方法来访问Logger及其管理的其他类。

Not detailed here: I'm using John Resig's Simple JavaScript Inheritance and RequireJS. 此处未详述:我正在使用John Resig的Simple JavaScript Inheritance和RequireJS。

Example 1: 范例1:

var Main = {
  init: function() {
    this.logger = new Logger();
    this.app = new App(this);
  }
}

var Logger = {
  debug: function(message) { 
    console.log(message) 
  }
}

var App = {
  init: function(main) {
    this.template = new Template(main);
  }
}

var Template = {
  init: function(main) {
    main.logger.debug("Hello");
  }
}

Example 2: 范例2:

var Main = {
  init: function() {
    this.logger = new Logger();
    this.app = new App(this);
  }
  ,debug: function(message) {
    this.logger.debug(message);
  }
}

var Logger = {
  debug: function(message) { 
    console.log(message);
  }
}

var App = {
  init: function(main) {
    this.main = main;
    this.template = new Template(this);
  }
  ,debug: function(message) {
    this.main.debug(message);
  }
}

var Template = {
  init: function(app) {
    app.debug("Hello");
  }
}

I found really useful in my OOP projects with JavaScript use the Dependency Injection pattern and Event Bus pattern. 我发现使用Dependency Injection模式和Event Bus模式在使用JavaScript的OOP项目中非常有用。 It helps a lot also for the Unit Test if you decouple the logic using MVC or MVP . 如果使用MVCMVP解耦逻辑,则对单元测试也有很大帮助。

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

相关问题 在JavaScript中使用正则表达式检查模式重用(例如,在字符串中声明两次的相同字符序列) - Using regular expressions in JavaScript for checking pattern reuse (e.g. the same sequence of characters declared twice in string) 用javascript控制视频(例如streamcloud) - Control Videos with javascript (e.g. streamcloud) 正则表达式匹配最后一次出现的模式(例如 - Regex match up to the LAST occurrence of a pattern (e.g. </div>) BEFORE another matching pattern (e.g. </div-container>) 如何在Javascript中将数组显示为视觉图像(例如迷宫) - How to display arrays as visuals in Javascript (e.g. for a maze) 全局对象(例如Array)是Javascript编程语言的一部分吗? - Is global objects (e.g. Array) are part of Javascript programming Language? 处理javascript正则表达式匹配项,例如,替换前为$ 1 - Manipulate a javascript regex match e.g. $1 before replacing javascript库中的methodObject符号(例如React和Sequelize) - methodObject notation in javascript libraries (e.g. React and Sequelize) 重用Javascript TypedArray(例如Uint32Array) - Reuse Javascript TypedArray (e.g. Uint32Array) 使用 JavaScript 以编程方式调度键盘(例如按键)事件 - Programmatically dispatch keyboard (e.g. keypress) events using JavaScript 用于DSL的javascript自动完成功能。 (例如:SQL) - javascript autocompletion for DSL. (e.g.: SQL)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM