道格拉斯克罗克福德的构造模式

ant*_*kas 22 javascript oop constructor

最近我看了Douglas Crockford的一次演讲(他的演讲让我着迷,但总是让我感到困惑).他给出了一个构造函数的例子,但我不太明白我将如何在实践中使用它:

function constructor(spec) {
  var that = other_constructor(spec),
    member,
    method = function () {
      //spec , member, method
    };

  that.method = method;
  return that;      
}
Run Code Online (Sandbox Code Playgroud)

也许有人可以根据这种模式给我一个简单的工作示例?

And*_*asE 16

这是Douglas Crockford的原始资料,因为它出现在他的幻灯片中:

function constructor(spec) {
  let {member} = spec,
      {other} = other_constructor(spec),
      method = function () {
        // member, other, method, spec
      };
  return Object.freeze({
    method,
    other
  });
}
Run Code Online (Sandbox Code Playgroud)

以下示例是Douglas Crockford的对象创建模式2014的更具体版本.

Douglas Crockford大量使用ECMAScript 6等功能,如解构等.!!

使用以下选项启动node.js中的代码(启用ES6):

node --harmony --harmony_destructuring demo.js
Run Code Online (Sandbox Code Playgroud)

demo.js

// Douglas Crockford 2014 Object Creation
(function() {
  'use strict';

  function adress(spec) {

    let {
      street, city
    } = spec,
    logAdress = function() {
      console.log('Adress:', street, city);
    };
    return Object.freeze({
      logAdress
    });
  };

  function person(spec) {

    let {
      preName,
      name
    } = spec, {
      logAdress
    } = adress(spec),
      logPerson = function() {
        // member, other, method, spec
        console.log('Name: ', preName, name);
        logAdress();
      };
    return Object.freeze({
      logPerson,
      logAdress
    });
  };


  let myPerson = person({
    preName: 'Mike',
    name: 'Douglas',
    street: 'Newstreet',
    city: 'London'
  });

  myPerson.logPerson();
})();
Run Code Online (Sandbox Code Playgroud)

根据Douglas Crockford的讲话,他避免使用:

  • 的Object.create
  • 这个 !!!

观看最初的Crockford视频:https://www.youtube.com/watch?v = PSGEjv3Tqo0

对于Crockford Douglas Object Creation Pattern 2014的一个很好的解释是这个博客:https://weblogs.asp.net/bleroy/crockford%E2%80%99s-2014-object-creation-pattern

  • 那里稳定.这样做符合Crockford的说法,它不需要新的前缀. (5认同)

Pit*_*taJ 15

这是在工厂函数中使用另一个构造函数来返回对象的示例.在这种情况下,other_constructor是构造函数,它正在创建一个类型的对象other_constructor(理想情况下,实际上这将是大写的).该对象存储在that.在此工厂函数中,method是一个已定义的函数,它被添加到that以某种方式扩展对象的功能.

构造函数和工厂函数之间的区别在于工厂函数只是返回对象的普通函数,而构造函数this指向新对象,并且通常必须使用new前面的关键字调用.

典型的构造函数:

function Dog(breed, height, name){
  this.breed = breed;
  this.animalType = "dog";
  this.height = height;
  this.name = name;
  // calling `return` isn't necessary here
}
Run Code Online (Sandbox Code Playgroud)

它的用法:

var lab = new Dog("labrador", 100, "Sugar"); // `new` is necessary (usually)
console.log(lab.animalType); // prints out "dog"
console.log(lab.height); // prints out 100
Run Code Online (Sandbox Code Playgroud)

典型的工厂功能:

function createDog(breed, height, name){
  var dog = {
    breed: breed,
    height: height,
    animalType: "dog",
    name: name
  };
  return dog; 
  // `return` is necessary here, because `this` refers to the 
  // outer scope `this`, not the new object
}
Run Code Online (Sandbox Code Playgroud)

它的用法:

var lab = createDog("labrador", 100, "Sugar"); // notice no need for `new`
console.log(lab.animalType); // prints out "dog"
console.log(lab.height); // prints out 100
Run Code Online (Sandbox Code Playgroud)

Eric Elliot的博客很好地解释了它们与不同用例之间的区别