将对象附加到Prototype而不会破坏DOM

时间:2017-08-29 01:31:15

标签: javascript jquery prototype

我正在编写一个库,我不确定如何保密(我不希望我的构造函数可用作全局对象)。

// This is a no-no.
var SecretObj = function() { ... }
SecretLib.prototype.SecretObj = SecretObj;

但当然,我可以这样做:

// This is a no-no.
SecretLib.prototype.SecretObj = function() { ... }

// Its even worse when I have more 
// prototypes on the SecretObj. Ex : 
SecretLib.prototype.SecretObj.prototype.wowitslong = function() { ... }

但是又一次不够好,所以我这样做了:

// For more short code.
var SecretLib.fn = SecretLib.prototype;

(function(){

  var SecretObj = function() { ... }
  SecretObj.fn = SecretObj.prototype;
  
  // And then
  SecretObj.fn.finally = function() { ... }
  
  // And  to finish
  SecretLib.fn.SecretObj = SecretObj;

})()

// But then, it becomes quite messy to write : 
SecretLib.SecretObj.finally

// Or something like
SecretLib.SecretObj.InnerSecretObj.omg();

我不知道它是否足够好或是否有更好的方法。谢谢你的帮助。

0 个答案:

没有答案