javascript - Problem accessing private variables in jQuery like chainable design pattern -


I'm trying to create my own custom toolbox which imitates jQuery's design pattern. Originally this idea has been derived from a certain degree: (check the answer given by "David").

Then here is my toolbox function:

  (function) {var myspace = function ()} New PrivateSpace ();} var PersonalSpace = function () {var testCache = {};}; Personal Spaces Prototype = {init: function () {console.log ('init this: this, back);), azax: function () {console.log (' call AJAX '); Back it;} cache: function (key, selector) {console.log ('cache selector here'); Testcache [key] = selector; Console.log ('cached selector:', testcache); Return it;}} window.hmis = window.m $ = mySpace ();}) (window)  

Now, if I implement this function like this:

  console.log (m $ .cache ('firstname', '#firstNameTextbox'));  

I get an error 'testcache' is not defined I am not able to use variable "testcache" inside the cache function of the prototype. How should I use it? Actually, what I want to do, I want to cache all my jQuery selectors in an object and want to use this object in the future.

Test cache is closed that makes new personal space to use here The correct pattern is

  var PersonalSpace = function () {this.testCache = {}; }; PrivateSpace.prototype = {cache: function (key, selector) {this.testcache [key] = selector; This return; }}  

here

But the code of the whole code seems a little bit - there is no reason to use a prototype pattern when only one instance is created. You should rely on the variable of access through shared charge (closing) instead.

  (function (window) (var cache = {}, mylib; window.mylib = mylib = {cache: function (key, selector) {cache [key] = selector; return mylib; }}}} (Window);  

update
and by the way,

update
Private space can be a better way

No P>

  function personalpace () {var cache = {}; return {cache: { Get: function (key) {return cache [key];}, set: function (key, value) {cache [key] = value;}}, init: function () {...}};}  

This is usually Mr. members are used to provide ..


Comments