2015-05-18 299 views
3

有時候在JavaScript中,我需要僞類許多構造函數和對象,因爲我喜歡的目標很讓我做這樣的事情:如何創建一個構造函數在javascript中創建構造函數?

var anyClass = (function (settings) { 
    var staticPrivate = {}, staticPublic = function() { 
     var public = this, private = {}; 
     (function constructor (here, the, args) { 
      this.hta = [here, the, args, arguments]; 
     }).apply(this, arguments); 
     arguments = undefined;delete arguments; 
     private.stuff = function() {} 
     Object.defineProperties(public, { 
      "e.g. length": { 
       get: function() { 
        return private.length; 
       }, 
       set: function (newValue) { 
        return; 
       }, 
       enumerable: false 
      } 
     }); 
    }; 
    Object.defineProperties(staticPublic, { 
     "staticFinalHiddenString": { 
      get: function() { 
       return "YEAH, I'm static and final and hidden..."; 
      }, 
      set: function (newValue) { 
       return "You cannot set me.. :P"; 
      }, 
      enumerable: false 
     } 
    }); 
    staticPrivate.some = function (init) { 
     if (settings.some == "settings") init(); 
    } 
    window.requestAnimationFrame(function() { 
     staticPrivate.some(function (I) { 
      run(on, first, render); 
     }); 
    }); 
    return staticPublic; 
})({ 
    some: "settings", 
    here: null 
}); 

而且,每一次,所以現在我想創建一個新的類的構造函數爲了我。我覺得這個:

new Class({ 
    constructor: function (here) { 
     is(my + constructor); 
    }, 
    properties: { 
     name: { 
      getter: function() {}, 
      setter: function (newValue) {}, 
      hidden: false, 
      static: false, 
      final: false 
     }, 
     version: { 
      getter: function() { 
       return 0.3; 
      }, 
      setter: function (newValue) {}, 
      hidden: true, 
      static: true, 
      final: true 
     } 
    } 
}); 

但我的問題是,我不知道如何創建具有構造class.prototype.prototype不起作用的原型/構造。

我只是去嘗試:

var Class = (function() { 
    var Class = (function() { 
     var constructor = function() { 
      return (function (information) { 
       this.prototype = {}; 
       var properties = {}; 
       for(var key in information.properties) { 
        properties[key] = { 
         get: information.properties[key].getter, 
         set: information.properties[key].setter, 
         enumerable: !information.properties[key].hidden || true 
        }; 
       }; 
       Object.defineProperties(this.prototype, properties); 
       return this; 
      }).apply(this, arguments); 
     }; 
     return constructor; 
    })(); 
    return Class; 
})(); 

這並不爲我工作:C

我希望你能幫助我。謝謝...

+4

Javascript不是面向對象的語言。每次我使用(或濫用)JavaScript作爲面向對象的語言,我都陷入了一片混亂。 –

+0

@ maze-le參見[面向對象的JavaScript簡介](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Introduction_to_Object-Oriented_JavaScript) – Aprillion

+6

@ maze-le:Javascript是一個面嚮對象語言。這不僅僅是一種基於班級的語言。 – slebetman

回答

0

我明白我的錯誤,現在我可以返回somthing,當我有一個構造函數。

var Class = function() {};和關閉功能Class.prototype.apply(this, arguments)做的事情,這就是爲什麼我可以在構造函數返回Class,如果我只想做

var Class = function() { 
    var ClassICreate = function() {}; 
    ... 
    return ClassICreat; 
} 

這是行不通的,因爲你不能從構造函數返回,因爲它是一個對象。


也就是說它是如何工作的我:

var Class = (function() { 
    var Class = function() { 
     return (function (information) { 
      var Class = function() {}; 
      var properties = {}; 
      for(var key in information.properties) { 
       properties[key] = { 
        get: information.properties[key].getter, 
        set: information.properties[key].setter, 
        enumerable: !information.properties[key].hidden || true 
       }; 
      }; 
      Object.defineProperties(Class.prototype, properties); 
      return Class; 
     }).apply(this, arguments); 
    }; 
    return Class; 
})(); 

後,我找到了答案,它看起來那麼容易了我,感謝您的意見,他們幫我找到合適的回答...