如何为传递给构造函数的对象设置默认属性?

时间:2022-05-20 19:06:48

I'm trying to make a change in one spot to affect the config object passed into all instantiations of an object. The object is made available globally as follows:

我试图在一个位置进行更改以影响传递到对象的所有实例化的配置对象。该对象在全球范围内可用,如下所示:

function Crayons(){
  return {
    foo: ThirdPartyFoo
  }
}

The object is initialized in my project with var myFoo = new Crayons().foo({color: "red"});

在我的项目中用var myFoo = new Crayons()初始化对象.foo({color:“red”});

I'd like to make {color: "blue"} the default, so that if someone doesn't pass in a color, blue is set.

我想将{color:“blue”}设为默认值,这样如果有人没有传入颜色,则设置为蓝色。

I tried doing

我试过了

function Crayons(){
  var fooWithDefaults = function(){
    this = new ThirdPartyFoo(arguments); //this is invalid
    this.color = "blue"; //and this would overwrite color if it was set
  }

  return {
    foo: fooWithDefaults
  }
}

But the new keyword is throwing me off, as I don't know how to create a javascript constructor that essentially says this = new 3rdPartyFoo.

但新的关键字让我失望,因为我不知道如何创建一个基本上说这个= new 3rdPartyFoo的javascript构造函数。

What am I missing?

我错过了什么?

2 个解决方案

#1


2  

You can either decorate the constructor:

你可以装饰构造函数:

function Crayons(){
  function fooWithDefaults() {
    3rdPartyFoo.apply(this, arguments); // this is what you're looking for
    if (!this.color) // or whatever to detect "not set"
      this.color = "blue";
  }
  fooWithDefaults.prototype = 3rdPartyFoo.prototype; // to make `new` work

  return {
    foo: fooWithDefaults
  }
}

Or you just make it a factory that returns an instance:

或者你只是让它成为一个返回实例的工厂:

function Crayons(){
  function fooWithDefaults(arg) {
    var that = new 3rdPartyFoo(arg); // you should know how many arguments it takes
    if (!that.color) // or whatever to detect "not set"
      that.color = "blue";
    return that;
  }

  return {
    foo: fooWithDefaults
  }
}

Here you can also drop the new when calling var myFoo = Crayons.foo({color: "red"});

在这里你也可以在调用var myFoo = Crayons.foo({color:“red”})时删除新的;

An alternative to modifying the instance after its creation would be to decorate the options that are passed in, which is in general the better solution:

在创建实例后修改实例的另一种方法是装饰传入的选项,这通常是更好的解决方案:

function Crayons(){
  function fooWithDefaults(arg) {
    if (!arg.color) // or whatever to detect "not set"
      arg.color = "blue";
    return new 3rdPartyFoo(arg);
  }

  return {
    foo: fooWithDefaults
  }
}

#2


0  

function ThirdPartyCrayon(config) {           // constructor :: possible original Crayon implementation

    Object.assign(this, config);
    this.type = "thirdpartycrayon";
}


function createCrayonSetting(defaultConfig) { // factory (creates a closure)

    function CrayonWithDefaults() {           // constructor :: customized Crayon wrapper

        Object.assign(this, defaultConfig);
        ThirdPartyCrayon.apply(this, arguments);
    }

    return {
        Crayon: CrayonWithDefaults
    }
}


var
    setting = createCrayonSetting({color: "blue", strokeWidth: "thin"}),

    crayon1 = new setting.Crayon({color: "red", strokeWidth: "bold"}),
    crayon2 = new setting.Crayon({color: "green"});
    crayon3 = new setting.Crayon();


console.log("crayon1 : ", crayon1);
console.log("crayon2 : ", crayon2);
console.log("crayon3 : ", crayon3);

#1


2  

You can either decorate the constructor:

你可以装饰构造函数:

function Crayons(){
  function fooWithDefaults() {
    3rdPartyFoo.apply(this, arguments); // this is what you're looking for
    if (!this.color) // or whatever to detect "not set"
      this.color = "blue";
  }
  fooWithDefaults.prototype = 3rdPartyFoo.prototype; // to make `new` work

  return {
    foo: fooWithDefaults
  }
}

Or you just make it a factory that returns an instance:

或者你只是让它成为一个返回实例的工厂:

function Crayons(){
  function fooWithDefaults(arg) {
    var that = new 3rdPartyFoo(arg); // you should know how many arguments it takes
    if (!that.color) // or whatever to detect "not set"
      that.color = "blue";
    return that;
  }

  return {
    foo: fooWithDefaults
  }
}

Here you can also drop the new when calling var myFoo = Crayons.foo({color: "red"});

在这里你也可以在调用var myFoo = Crayons.foo({color:“red”})时删除新的;

An alternative to modifying the instance after its creation would be to decorate the options that are passed in, which is in general the better solution:

在创建实例后修改实例的另一种方法是装饰传入的选项,这通常是更好的解决方案:

function Crayons(){
  function fooWithDefaults(arg) {
    if (!arg.color) // or whatever to detect "not set"
      arg.color = "blue";
    return new 3rdPartyFoo(arg);
  }

  return {
    foo: fooWithDefaults
  }
}

#2


0  

function ThirdPartyCrayon(config) {           // constructor :: possible original Crayon implementation

    Object.assign(this, config);
    this.type = "thirdpartycrayon";
}


function createCrayonSetting(defaultConfig) { // factory (creates a closure)

    function CrayonWithDefaults() {           // constructor :: customized Crayon wrapper

        Object.assign(this, defaultConfig);
        ThirdPartyCrayon.apply(this, arguments);
    }

    return {
        Crayon: CrayonWithDefaults
    }
}


var
    setting = createCrayonSetting({color: "blue", strokeWidth: "thin"}),

    crayon1 = new setting.Crayon({color: "red", strokeWidth: "bold"}),
    crayon2 = new setting.Crayon({color: "green"});
    crayon3 = new setting.Crayon();


console.log("crayon1 : ", crayon1);
console.log("crayon2 : ", crayon2);
console.log("crayon3 : ", crayon3);