How to “properly” create a custom object in JavaScript?

后端 未结 15 2006
被撕碎了的回忆
被撕碎了的回忆 2020-11-21 08:07

I wonder about what the best way is to create an JavaScript object that has properties and methods.

I have seen examples where the person used var self = this<

15条回答
  •  無奈伤痛
    2020-11-21 08:36

    I'd like to mention that we can use either a Title or a String to declare an Object.
    There are different ways on calling each type of them. See below:

    var test = {
    
      useTitle : "Here we use 'a Title' to declare an Object",
      'useString': "Here we use 'a String' to declare an Object",
      
      onTitle : function() {
        return this.useTitle;
      },
      
      onString : function(type) {
        return this[type];
      }
      
    }
    
    console.log(test.onTitle());
    console.log(test.onString('useString'));

提交回复
热议问题