JavaScript constructors using JavaScript object literal notation

前端 未结 6 1730
半阙折子戏
半阙折子戏 2021-01-30 15:30

What is the best way to build constructors in JavaScript using object literal notation?

var myObject = {
 funca : function() {
  //...
 },

 funcb : function() {         


        
6条回答
  •  孤城傲影
    2021-01-30 15:31

    var myObject = function(arg){
        return{
            constructor: function(arg){
                //...
                return this;
            },
    
            funca: function(){
                //...
            },
    
            funcb: function(){
                //...
            }
        }.constructor(arg);
    };
    
    //...
    
    var myVar = new myObject("...");
    

提交回复
热议问题