JavaScript中的这种设计模式是什么? [英] What is this design pattern known as in JavaScript?

查看:73
本文介绍了JavaScript中的这种设计模式是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在查看 Scrabb.ly 的js源代码。

I was looking over the js source code for Scrabb.ly.

我注意到他们会为每个不同的类做类似的事情:

I've noticed that they would do something like so for each of their distinct "classes":

var Board = (function() {
  var self = {};

  // settings for board
  self.options = {
    debug: true,
    addedPlayTiles: function() {},
    clearedPlayTiles: function() {}
  };

  // set to true once the board has been setup
  self.isSetup = false;

  // quick access to square elements
  self.squares = {};
  self.squareCount = 0;

  self.setup = function(options) {
    self.log("Setting up board!");

    // set options
    _.each(options, function(val, key) {
      self.options[key] = val;
    });

    return self;
})();

中间的一些代码已被省略,但这应该给你一般的想法。

Some code from the middle has been omitted but this should give you the general idea.


  1. 以下内容的目的是什么:(function(){// code})(); 这是我见过的模块模式吗?这是否意味着保持全局命名空间的清洁?

  2. 这一行是什么意思?: var self = {} 是自我对象吗?过去暴露'公众'成员?你会如何定义私有函数或变量?

  3. 如果你愿意,你会如何实例化多个Boards?

  1. What is the purpose of the the following: (function() { // code })(); Is this the module pattern that I've seen talked about? Is this meant to keep the global namespace clean?
  2. What does this line mean?: var self = {} Is the self object used to exposed 'public' members? How would you define a private function or variable?
  3. How would you instantiate multiple "Boards" if you wanted to?


推荐答案

它被称为模块模式。

函数周围的括号表示它在被评估后立即被评估定义 - 所以本质上它是 Singleton 。由于它是一个匿名函数,因此不会存储定义 - 所以你不能轻易地创建这个对象的新实例而不做一些修改(稍后会讨论)。

The parentheses around the function mean it's being evaluated immediately after being defined -- so in essence it's a Singleton. Since it's an anonymous function, the definition is not stored - so you cannot readily create new instances of this object without a few modifications (which will be discussed later).

你是正确的, self 包含公共方法和属性。由于闭包属性,任何未在 self 中定义的变量都不会被外部看到。但是, self 中定义的任何函数仍然可以访问私有变量,因为在Javascript中,函数可以访问定义它们的上下文(包括变量) - 少数例外..主要是参数这个

You are correct, self contains the "public" methods and properties, as it were. Any variables that aren't defined in self are not visible to the outside because of the closure properties. However, any functions defined in self still have access to the private variables because in Javascript, functions maintain access to the context (including variables) in which they were defined -- with a few exceptions.. mainly arguments and this.

如果要定义此对象的多个实例,则应删除括号( var Board = function(){...} ),然后使用 var obj = Board()创建一个对象。请注意,它不使用 new 运算符。

If you want to define multiple instances of this object, you would remove the parentheses (var Board = function () { ... }) and then use var obj = Board() to create an object. Note that it does not use the new operator.

这篇关于JavaScript中的这种设计模式是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆