最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - TypeScript - Pass object literal to class constructor - Stack Overflow

programmeradmin1浏览0评论

Can you pass default values to a class constructor? Here, last returns undefined.

class greatFunction {
    options: {
        names:  {first: 'Joe', last: 'Bloggs'}
    }
    constructor(options) {
        console.log(options.names.first + ' ' + options.names.last);        
    }
}

Called:

new greatFunction({
    names: {
        first: 'Paul'
    }
});

Can you pass default values to a class constructor? Here, last returns undefined.

class greatFunction {
    options: {
        names:  {first: 'Joe', last: 'Bloggs'}
    }
    constructor(options) {
        console.log(options.names.first + ' ' + options.names.last);        
    }
}

Called:

new greatFunction({
    names: {
        first: 'Paul'
    }
});
Share Improve this question asked Aug 22, 2016 at 10:44 Paul RedmondPaul Redmond 3,2964 gold badges35 silver badges53 bronze badges 3
  • I am not familiar with typescript but, within your constructor you could set the default by using options.names.last = options.name.last || "default" if the first side of the check (left from ||) is true, that value is taken, if it is false, the right side is taken. Since undefined is false, it defaults to "default" – Dellirium Commented Aug 22, 2016 at 10:48
  • @Dellirium Yup I know that but looking for tidier way of doing it. Thanks! – Paul Redmond Commented Aug 22, 2016 at 10:49
  • You don't want your param name to be the same as a local variable – Andy-Delosdos Commented Aug 22, 2016 at 10:50
Add a ment  | 

3 Answers 3

Reset to default 3

instead of doing that why dont you create an interface like this

interface names { 
      firstname:string;
      lastname ?: string;
  }

Since you can't create make a property of a parameter optional.

Or you do the hard way which is like this

interface IOptions{
names:INames;
     }

 interface  INames {
    first:string;
    last?:string;
  }

   class greatFunction{

        constructor (options:IOptions){
          options.names.last = options.names.last?options.names.last:'Default';
       console.log(options.names.first + ' ' + options.names.last)
    }
 }

   new greatFunction({names:{first:'Theophilus'}});

It should return the default in console

Try setting default values:

class GreatFunction {
  options: {
    names:  {first: 'Joe', last: 'Bloggs'}
  }
  constructor({names: {first=this.options.names.first: firstName, last=this.options.names.last: lastName}}) {
    console.log(`${firstName} ${lastName}`)        
  }
}

Alternatively, use a library, for example lodash.defaults() seems to fit your use case.

You do need to pass something to the function, so one option would be to try something like this:

class greatFunction {
    defaultOptions = {
        names: { first: 'Joe', last: 'Bloggs' }
    }
    constructor(options) {
        if (options == null)
            options = this.defaultOptions;
        console.log(options.names.first + ' ' + options.names.last);
    }
} 

You can then create a new greatFunction using:

let x = new greatFunction(null)

Or:

let y = new greatFunction({ names: { first: 'Test', last: 'Blah' } });

Alternatively, don't do it using the constructor, and use a property after initialising. For example:

class greatFunction {
    public options = {
        names: { first: 'Joe', last: 'Bloggs' }
    }
    constructor() { }
    doSomething() {
        console.log(this.options.names.first + ' ' + this.options.names.last);
    }
} 

and then use with:

let x = new greatFunction();
x.options = { names: { first: 'Test', last: 'Blah' } };
x.doSomething();
发布评论

评论列表(0)

  1. 暂无评论